梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
1.导入dom4j-1.6.1.jar
2.导入jaxen-1.1.1.jar
package com.hwp.test;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import org.dom4j.io.*;

/**
 * 
@author 惠万鹏
 * 
 
*/

public class XmlTest
{
    
/**
     * <p>
     * 写XML文件
     * </p>
     * 
     * 
@param fileName
     *            文件名
     * 
@param document
     *            文档
     * 
@return isWritedSuccessful 写XML是否成功
     
*/

    
public static boolean writeXML(String fileName, Document document)
    
{
        
boolean isWritedSuccessful = false;
        XMLWriter writer 
= null;
        
try
        
{
            OutputFormat format 
= OutputFormat.createPrettyPrint();
            format.setEncoding(
"UTF-8");
            writer 
= new XMLWriter(new FileOutputStream(fileName), format);
            writer.write(document);
            isWritedSuccessful 
= true;
        }

        
catch (Exception e)
        
{
            e.printStackTrace();
        }

        
finally
        
{
            
if (writer != null)
            
{
                
try
                
{
                    writer.close();
                }

                
catch (Exception e)
                
{
                    e.printStackTrace();
                }

            }

        }

        
return isWritedSuccessful;
    }

    
    
/**
     * <p>
     * 根据文件名创建XML
     * </p>
     * 
     * 
@param fileName
     *            文件名
     * 
@return 是否创建成功
     
*/

    
public static boolean createStudentXML(String fileName)
    
{
        Document document 
= DocumentHelper.createDocument();
        document.addComment(
"99级1班学生信息");
        document.addElement(
"students");
        
return XmlTest.writeXML(fileName, document);
    }

    
    
/**
     * <p>
     * 根据文件名得到Document
     * </p>
     * 
     * 
@param fileName
     *            文件名
     * 
@return document
     
*/

    
public static Document getStudentXMLDocument(String fileName)
    
{
        SAXReader saxReader 
= new SAXReader();
        
try
        
{
            
return saxReader.read(new File(fileName));
        }

        
catch (Exception e)
        
{
            e.printStackTrace();
        }

        
return null;
    }

    
    
/**
     * <p>
     * 向XML里插入一条信息
     * </p>
     * 
     * 
@param fileName
     * 
@param id
     * 
@param name
     * 
@return
     
*/

    
public static boolean addStudentInfo(String fileName, String id, String name)
    
{
        Document document 
= XmlTest.getStudentXMLDocument(fileName);
        
if (document == null)
        
{
            
return false;
        }

        
        Element eStudents 
= document.getRootElement();
        Element eStudent 
= eStudents.addElement("student");
        eStudent.addAttribute(
"id", id);
        Element eName 
= eStudent.addElement("name");
        eName.setText(name);
        
return XmlTest.writeXML(fileName, document);
    }

    
    
/**
     * <p>
     * 根据id删除相应的student信息
     * </p>
     * 
     * 
@param fileName
     *            文件名
     * 
@param id
     *            studentId
     * 
@return
     
*/

    
public static boolean deleteStudentInfo(String fileName, String id)
    
{
        
boolean isDeleteSuccessful = false;
        Document document 
= XmlTest.getStudentXMLDocument(fileName);
        
if (document == null)
        
{
            
return false;
        }

        List list 
= document.selectNodes("/students/student[@id='" + id + "']");
        System.out.println(list);
        
if (list != null)
        
{
            Iterator it 
= list.iterator();
            
while (it.hasNext())
            
{
                Element eStudent 
= (Element) it.next();
                
if (eStudent != null)
                
{
                    eStudent.getParent().remove(eStudent);
                }

                isDeleteSuccessful 
= document.remove(eStudent);
            }

        }

        
if (isDeleteSuccessful)
        
{
            
return XmlTest.writeXML(fileName, document);
        }

        
return isDeleteSuccessful;
    }

    
    
/**
     * <p>
     * 解析xml
     * </p>
     * 
     * 
@param fileName
     *            文件名
     * 
@return
     
*/

    
public static List<ArrayList<String>> getStudentInfo(String fileName)
    
{
        List
<ArrayList<String>> students = null;
        students 
= new ArrayList<ArrayList<String>>();
        
        Document document 
= XmlTest.getStudentXMLDocument(fileName);
        List eStudentList 
= document.selectNodes("/students/*");
        
if (eStudentList != null)
        
{
            Iterator itEStudentList 
= eStudentList.iterator();
            ArrayList
<String> student = null;
            
while (itEStudentList.hasNext())
            
{
                student 
= new ArrayList<String>();
                Element eStudent 
= (Element) itEStudentList.next();
                student.add(eStudent.attributeValue(
"id"));
                student.add(eStudent.element(
"name").getTextTrim());
                students.add(student);
            }

        }

        System.out.println(students);
        
return students;
    }

    
    
public static void main(String args[])
    
{
        String fileName 
= "c:/stu.xml";
        XmlTest.createStudentXML(fileName);
         
for(int i=0;i<10;i++)
             XmlTest.addStudentInfo(fileName,String.valueOf(i),
"惠万鹏" + i);
         
//XmlTest.deleteStudentInfo(fileName,"4");
        XmlTest.getStudentInfo(fileName);
    }

}

posted on 2008-06-16 21:18 HUIKK 阅读(501) 评论(0)  编辑  收藏 所属分类: XML

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问