DOM4j解析XML

来源:互联网 发布:极速mac修改器 编辑:程序博客网 时间:2024/06/05 17:10
package com.demo.xmldemo;import com.demo.xmldemo.bean.Student;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.xml.sax.XMLReader;import javax.sql.rowset.spi.XmlWriter;import java.io.*;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * Created with IntelliJ IDEA. * User: a549238 * Date: 3/14/13 * Time: 1:07 PM * To change this template use File | Settings | File Templates. */public class XMLManagerByDom4j {    private  final String filepath;    public XMLManagerByDom4j(String filepath)    {        this.filepath=filepath;    }    /**     * init  XML  File     * @param strudentList     */    public  void createXMLFile(List<Student> strudentList)    {        Document document= DocumentHelper.createDocument();        Element root=DocumentHelper.createElement("root");        document.setRootElement(root);         //        设置root节点的另一种方法:document.addElement("root") ;        Element element=null;        for(Student student:strudentList)        {            element=root.addElement("student");            element.addElement("name").addText(student.getName());            element.addElement("sex").addText(student.getSex());            element.addElement("age").addText(String.valueOf(student.getAge()));        }        root.appendContent(element);  //这是添加元素的另一种方式        saveFile(document,filepath);    }    public List<Student> getAllStudents()    {        List<Student> studentList=new ArrayList<Student>();        try {            SAXReader saxReader=new SAXReader();            Document document=saxReader.read(new File(filepath)) ;            Element root=document.getRootElement();            Iterator<Element> elementIterator=root.elementIterator("student");            Element element=null,tempelement2=null;            while(elementIterator.hasNext())            {                Student student=new Student();                element=elementIterator.next();                for(Iterator<Element> tempelement=element.elementIterator();tempelement.hasNext();)                {                    tempelement2=tempelement.next();                    if(tempelement2.getName().equals("name"))                        student.setName(tempelement2.getText());                    else if(tempelement2.getName().equals("sex"))                        student.setSex(tempelement2.getText());                    else if(tempelement2.getName().equals("age"))                        student.setAge(Integer.parseInt(tempelement2.getText()));                    studentList.add(student) ;                }            }        }catch (DocumentException e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        }        return studentList;    }    public  Student getStudentByName(String name)    {        List<Student> studentList=getAllStudents();        for(Student student:studentList)            if(student.getName().equals(name))                return student;        return null;    }    public void delByName(String name)    {        try {            SAXReader saxReader=new SAXReader();            Document document=saxReader.read(new File(filepath)) ;            Element root=document.getRootElement();            Iterator<Element> elementIterator=root.elementIterator("student");            Element element=null,tempelement2=null;            while(elementIterator.hasNext())            {                Student student=new Student();                element=elementIterator.next();                for(Iterator<Element> tempelement=element.elementIterator();tempelement.hasNext();)                {                    tempelement2=tempelement.next();                    if(tempelement2.getName().equals("name"))                        if(tempelement2.getText().equals(name))                            tempelement.remove();                }            }            saveFile(document,filepath);        }catch (DocumentException e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        }    }    private void saveFile(Document document,String savefilepath)    {        try {            OutputFormat outputFormat=OutputFormat.createPrettyPrint();            XMLWriter xmlWriter=new XMLWriter(new FileOutputStream(new File(savefilepath)),new OutputFormat("   ",true,"UTF-8"));            xmlWriter.write(document);           // xmlWriter.flush();            xmlWriter.close();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        } catch (FileNotFoundException e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        } catch (IOException e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        }    }}
啥也不说了,悲催
原创粉丝点击