首页 > 学院 > 开发设计 > 正文

dom4j解析xml

2019-11-08 02:56:22
字体:
来源:转载
供稿:网友

一 解析xml文件如下:

<?xml version="1.0" encoding="UTF-8"?><students>   <student1 id="001">     <微信公众号>@残缺的孤独</微信公众号>      <学号>20140101</学号>      <地址>北京海淀区</地址>      <座右铭>要么强大,要么听话</座右铭>      <phone>137xxxxxxxx</phone>      <phone>137xxxxxxxx</phone>    <QQ>786363617@qq.com</qq>  </student1>    <student2 id="002">     <新浪微博>@残缺的孤独</新浪微博>      <学号>20140102</学号>      <地址>北京朝阳区</地址>      <座右铭>在哭泣中学会坚强</座右铭>   </student2> </students>

二 :解析xml步骤如下

/** *  */package cn.com.yy.dom4j;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.junit.Test;/** * @author niu * */public class Dom4JforXML {/*@Testpublic void test() throws DocumentException{//创建SAXReader对象 SAXReader reader = new SAXReader();//读取文件 转换成DocumentDocument document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));//获取根节点元素对象Element root = document.getRootElement();//遍历listNode(root);}*///遍历当前节点下所有的节点public void listNode(Element root) {// TODO Auto-generated method stubSystem.out.PRintln("当前节点的名称"+root.getName());//首先获取当前节点的所有属性节点List<Attribute> list = root.attributes();//遍历属性节点for (Attribute attribute : list) {System.out.println("属性:"+attribute.getName()+":"+attribute.getValue());}//如果当前节点内容不为空,则输出if (!(root.getTextTrim().equals(""))) {System.out.println(root.getName()+":"+root.getText());}//同时迭代当前节点下面的所有子节点Iterator<Element> iterator = root.elementIterator();while (iterator.hasNext()) {Element e = iterator.next();listNode(e);}}//添加节点属性、删除节点属性、修改属性值/* @Testpublic void test2() throws DocumentException{SAXReader reader = new SAXReader();Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));Element root = document.getRootElement();System.out.println("--------添加属性前-------------------");Element student1Element = root.element("student1");listNode(student1Element);Attribute idAttribute = student1Element.attribute("id");student1Element.remove(idAttribute);student1Element.addAttribute("name", "这是student1节点的新属性");System.out.println("-----------------添加新属性后----------------");listNode(student1Element);}*///删除指定节点,新增节点操作@Testpublic void test3() throws DocumentException, IOException{//创建SAXReader对象          SAXReader reader = new SAXReader();          //读取文件 转换成Document          Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));          //获取根节点元素对象          Element root = document.getRootElement();          System.out.println("-------添加节点前------");          //获取节点student1          Element student1Element = root.element("student1");          //遍历          listNode(student1Element);          //添加phone节点          Element phoneElement = student1Element.addElement("phone");          Element qqElement = student1Element.addElement("qq");        //为phone节点设置值          phoneElement.setText("137xxxxxxxx");          qqElement.setText("786363617@qq.com");        System.out.println("-------添加节点后------");          listNode(student1Element);          //把student1Element写入新文件          writerDocumentToNewFile(document);          System.out.println("---写入完毕----");  }public void writerDocumentToNewFile(Document document) throws IOException {// TODO Auto-generated method stub//输出格式OutputFormat  format= OutputFormat.createPrettyPrint();//设置编码format.setEncoding("UTF-8");//XMLWriter 指定输出文件以及格式 XMLWriter write = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src/cn/com/yy/dom4j/s.xml")),"UTF-8"),format);//写入新文件write.write(document);write.flush();write.close();}}

三 解析xml有五种方法并比较如下

xml解析五种方法为  DOM    SAX  JDOM  DOM4j  XPath

xml五种方法比较如下:1、

【DOM】DOM是基于树的结构,通常需要加载整文档和构造DOM树,然后才能开始工作。优点:    a、由于整棵树在内存中,因此可以对xml文档随机访问    b、可以对xml文档进行修改操作    c、较sax,dom使用也更简单。缺点:    a、整个文档必须一次性解析完    a、由于整个文档都需要载入内存,对于大文档成本高

2、【SAX】SAX类似流媒体,它基于事件驱动的,因此无需将整个文档载入内存,使用者只需要监听自己感兴趣的事件即可。优点:    a、无需将整个xml文档载入内存,因此消耗内存少    b、可以注册多个ContentHandler缺点:    a、不能随机的访问xml中的节点    b、不能修改文档

3、【JDOM】JDOM是纯Java的处理XML的API,其API中大量使用Collections类,优点:    a、DOM方式的优点    b、具有SAX的Java规则缺点    a、DOM方式的缺点

4、【DOM4J】这4中xml解析方式中,最优秀的一个,集易用和性能于一身。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表