XML 的 Dom4j 解析范例,以及 XPath
2013-08-22 23:34
423 查看
使用的还是以前的book.xml文档
book.xml
看例子:
Dom4jDemo.java
book.xml
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>Think in Java</书名> <作者>海竹</作者> <售价>30.0</售价> </书> <书> <书名 ISBN码="521">WEB</书名> <作者>西行</作者> <售价>29.9</售价> </书> </书架>
看例子:
Dom4jDemo.java
package com.haizhu.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Dom4jDemo { public static void read() throws DocumentException{ // 得到解析器 SAXReader reader = new SAXReader(); // 得到 document Document document = reader.read(new File("src/com/haizhu/xml/book.xml")); // 得到根节点 Element root = document.getRootElement(); // 得到根节点的子节点是一个List数组,根据下标取得指定的子节点 Element book = (Element)root.elements("书").get(1); // 取得子节点的内容 String bookName = book.element("书名").getText(); // 取得子节点的属性内容 String attribute = book.element("书名").attributeValue("ISBN码"); System.out.println(bookName); System.out.println(attribute); } // 给书添加一个 售价 public static void add() throws DocumentException, IOException{ // 得到解析器 SAXReader reader = new SAXReader(); // 得到 document Document document = reader.read(new File("src/com/haizhu/xml/book.xml")); // 得到“书”这个节点 Element book = (Element) document.getRootElement().elements("书").get(1); // 添加给书添加一个“售价” book.addElement("售价").setText("88.8元"); // 一定不要忘记提交到 XML 文件 // 另外,之所以输出流转化这么多次,是为了指定编码 XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/com/haizhu/xml/book.xml"),"UTF-8")); writer.write(document); writer.close(); } // 认识 XPath // 除了上面导入的 dom4j-1.6.1.jar,还需要 jaxen-1.1-beta-6.jar public static void getSelectNode() throws DocumentException, IOException{ // 得到解析器 SAXReader reader = new SAXReader(); // 得到 document Document document = reader.read(new File("src/com/haizhu/xml/book.xml")); // 得到所有的“书名”节点 List listName = document.selectNodes("//书名"); // 得到所有的“书名”且包含“ISBN码”的属性的节点 List listNameAndAttr = document.selectNodes("//书名[@ISBN码]"); // 得到第一个“书名”节点 Node nodeName = document.selectSingleNode("//书名"); // 得到第一个“书名”且包含“ISBN码”的属性的节点 Node nodeNameAndAttr = document.selectSingleNode("//书名[@ISBN码]"); System.out.println(nodeName.getText()); System.out.println(nodeNameAndAttr.getText()); // 有一个问题,为什么不能遍历打印呢?而是一个死循环!!! Iterator itr = listName.iterator(); for(int i=0;i<listName.size();i++){ Element el = (Element)listName.iterator().next(); System.out.println(el.getName()+":"+el.getText()); } while(itr.hasNext()){ Element el = (Element)listName.iterator().next(); System.out.println(el.getName()+":"+el.getText()); } for(int i=0;i<listNameAndAttr.size();i++){ System.out.println(listNameAndAttr.iterator().next()); } } public static void main(String[] args) throws DocumentException, IOException{ getSelectNode(); } }
相关文章推荐
- javaweb学习总结十三(dom4j方式对XML文档进行解析以及Xpath的使用)
- 使用DOM4J解析XML文档,以及使用XPath提取XML文档
- XML介绍以及XML解析入门---DOM4J解析、XPath解析
- 使用dom4j解析xml文档以及用XPath实现相关的操作
- 使用DOM4J解析XML文档,以及使用XPath提取XML文档
- dom4j使用XPath解析XML返回null的解决方法
- C#解析XML详解(XPath以及带命名空间NameSpace)
- xpath技术解析xml以及案例模拟用户登录效果
- 采用Dom4j及xpath解析xml文件
- xpath技术解析xml以及案例模拟用户登录效果
- Xpath dom4j解析带有命名空间的XML的方法
- 简单xml的使用以及xml的解析dom4j和jaxp
- XML解析【介绍、DOM、SAX详细说明、jaxp、dom4j、XPATH】
- 使用dom4j的xPath解析XML
- 使用dom4j和XPath解析XML之例子二
- Java xml 操作(Dom4J修改xml   + xPath技术  + SAX解析 + XML约束)
- xml解析dom4j xpath方式
- Java xml 操作(Dom4J修改xml   + xPath技术  + SAX解析 + XML约束)
- dom4j和xpath解析带名称空间的xml…
- dom4j用XPath解析xml 遇到的问题