DOM4J Analysis
Features:
1. An intelligent branch of JDOM that incorporates many functions beyond basic XML document representation.
2. It uses interfaces and abstract basic class methods.
3. It has the characteristics of excellent performance, good flexibility, powerful functions and extreme ease of use.
4. It is an open source file
jar package: dom4j-1.6.1.jar
Create book.xml:
1 package com.example.xml.dom4j; 2 3 import java.io.FileWriter; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentHelper; 6 import org.dom4j.Element; 7 import org.dom4j.io.OutputFormat; 8 import org.dom4j.io.XMLWriter; 9 /**10 * dom4j创建xml文档示例11 *12 */13 public class Dom4JTest4 {14 public static void main(String[] args) throws Exception {15 // 第二种方式:创建文档并设置文档的根元素节点16 Element root2 = DocumentHelper.createElement("bookstore");17 Document document2 = DocumentHelper.createDocument(root2);18 19 // 添加一级子节点:add之后就返回这个元素20 Element book1 = root2.addElement("book");21 book1.addAttribute("id", "1");22 book1.addAttribute("name", "第一本书");23 // 添加二级子节点24 book1.addElement("name").setText("遇见未知的自己");25 book1.addElement("author").setText("张德芬");26 book1.addElement("year").setText("2014");27 book1.addElement("price").setText("109");28 // 添加一级子节点29 Element book2 = root2.addElement("book");30 book2.addAttribute("id", "2");31 book2.addAttribute("name", "第二本书");32 // 添加二级子节点33 book2.addElement("name").setText("双城记");34 book2.addElement("author").setText("狄更斯");35 book2.addElement("year").setText("2007");36 book2.addElement("price").setText("29");37 38 // 设置缩进为4个空格,并且另起一行为true39 OutputFormat format = new OutputFormat(" ", true);40 41 // 另一种输出方式,记得要调用flush()方法,否则输出的文件中显示空白42 XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("book.xml"),format);43 xmlWriter3.write(document2);44 xmlWriter3.flush();45 // close()方法也可以46 47 }48 }
Run result:
Parse book.xml:
1 package com.example.xml.dom4j; 2 3 import java.io.File; 4 import java.util.Iterator; 5 import java.util.List; 6 import org.dom4j.Attribute; 7 import org.dom4j.Document; 8 import org.dom4j.DocumentException; 9 import org.dom4j.Element;10 import org.dom4j.io.SAXReader;11 /**12 * dom4j解析xml文档示例13 *14 */15 public class Dom4JTest3 {16 17 public static void main(String[] args) {18 // 解析books.xml文件19 // 创建SAXReader的对象reader20 SAXReader reader = new SAXReader();21 try {22 // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。23 Document document = reader.read(new File("book.xml"));24 // 通过document对象获取根节点bookstore25 Element bookStore = document.getRootElement();26 System.out.println("根节点名:"+bookStore.getName());27 // 通过element对象的elementIterator方法获取迭代器28 Iterator it = bookStore.elementIterator();29 // 遍历迭代器,获取根节点中的信息(书籍)30 while (it.hasNext()) {31 System.out.println("=====开始遍历子节点=====");32 Element book = (Element) it.next();33 System.out.println("子节点名:"+book.getName());34 // 获取book的属性名以及 属性值35 List<Attribute> bookAttrs = book.attributes();36 for (Attribute attr : bookAttrs) {37 System.out.println("属性名:" + attr.getName() + "--属性值:"38 + attr.getValue());39 }40 Iterator itt = book.elementIterator();41 while (itt.hasNext()) {42 Element bookChild = (Element) itt.next();43 System.out.println("节点名:" + bookChild.getName() + "--节点值:" + bookChild.getStringValue());44 }45 System.out.println("=====结束遍历该节点=====");46 }47 } catch (DocumentException e) {48 e.printStackTrace();49 }50 }51 52 }
Run result:
The above is the detailed content of dom4j creation and parsing xml document example tutorial. For more information, please follow other related articles on the PHP Chinese website!