运行下面例子,我们要上网上下载dom4j的jar包。(如:dom4j-1.6.1)
通过dom4j来解析XML,详见例子:
通过dom4j生成xml
package com.dom4j; import java.io.FileOutputStream; import java.io.FileWriter; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class Dom4j { public static void main(String[] args) throws Exception { // 创建文档并设置文档的根元素节点 :第一种方式 // Document document = DocumentHelper.createDocument(); // // Element root = DocumentHelper.createElement("student"); // // document.setRootElement(root); // 创建文档并设置文档的根元素节点 :第二种方式 Element root = DocumentHelper.createElement("student"); Document document = DocumentHelper.createDocument(root); root.addAttribute("name","zhangsan"); Element helloElement = root.addElement("hello"); Element worldElement = root.addElement("world"); helloElement.setText("hello"); worldElement.setText("world"); helloElement.addAttribute("age","20"); XMLWriter xmlWriter = new XMLWriter(); xmlWriter.write(document); OutputFormat format = new OutputFormat(" ",true); XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream( "student2.xml"),format); xmlWriter2.write(document); XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"),format); xmlWriter3.write(document); xmlWriter3.close(); } }
XML配置文件:
<?xml version="1.0" encoding="utf-8"?> <学生名册 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="student.xsd" > <学生 学号="1"> <姓名>张三</姓名> <性别>男</性别> <年龄>20</年龄> </学生> <学生 学号="2"> <姓名>李四</姓名> <性别>女</性别> <年龄>19</年龄> </学生> <学生 学号="3"> <姓名>王五</姓名> <性别>男</性别> <年龄>21</年龄> </学生> </学生名册>
输出结果:
<?xml version="1.0" encoding="UTF-8"?> <student name="zhangsan"><hello age="20">hello</hello><world>world</world></student>
通过dom4j解析xml
package com.dom4j; import java.io.File; import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.DOMReader; import org.dom4j.io.SAXReader; public class Dom4j2 { public static void main(String[] args) throws Exception { SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(new File("student.xml")); Element root = doc.getRootElement(); System.out.println("root element: " + root.getName()); List childList = root.elements(); System.out.println(childList.size()); // List childList2 = root.elements("学生"); // // System.out.println(childList2.size()); // Element first = root.element("学生"); // // System.out.println(first.attributeValue("学号")); for (Iterator iter = root.elementIterator(); iter.hasNext();) { Element e = (Element) iter.next(); System.out.println(e.attributeValue("学号")); for (Iterator iter1 = e.elementIterator(); iter1.hasNext();) { Element e1 = (Element) iter1.next(); System.out.println(e1.getName() + "=" + e1.getText()); } } System.out.println("---------------------------"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document document = db.parse(new File("student.xml")); DOMReader domReader = new DOMReader(); // 将JAXP的Document转换为dom4j的Document Document d = domReader.read(document); Element rootElement = d.getRootElement(); System.out.println(rootElement.getName()); } }
xml同上面xml一致。
解析结果:
root element: 学生名册 3 1 姓名=张三 性别=男 年龄=20 2 姓名=李四 性别=女 年龄=19 3 姓名=王五 性别=男 年龄=21 --------------------------- 学生名册