01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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 Dom4JTest1 { public static void main(String[] args) throws Exception { // 第一种方式:创建文档,并创建根元素 // 创建文档:使用了一个Helper类 Document document = DocumentHelper.createDocument(); // 创建根节点并添加进文档 Element root = DocumentHelper.createElement("student"); document.setRootElement(root); // 第二种方式:创建文档并设置文档的根元素节点 Element root2 = DocumentHelper.createElement("student"); Document document2 = DocumentHelper.createDocument(root2); // 添加属性 root2.addAttribute("name", "zhangsan"); // 添加子节点:add之后就返回这个元素 Element helloElement = root2.addElement("hello"); Element worldElement = root2.addElement("world"); helloElement.setText("hello Text"); worldElement.setText("world text"); // 输出 // 输出到控制台 XMLWriter xmlWriter = new XMLWriter(); xmlWriter.write(document); // 输出到文件 // 格式 OutputFormat format = new OutputFormat(" ", true);// 设置缩进为4个空格,并且另起一行为true XMLWriter xmlWriter2 = new XMLWriter( new FileOutputStream("student.xml"), format); xmlWriter2.write(document2); // 另一种输出方式,记得要调用flush()方法,否则输出的文件中显示空白 XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student2.xml"), format); xmlWriter3.write(document2); xmlWriter3.flush(); // close()方法也可以 } } |
01 02 03 04 05 06 07 08 09 10 | <?xml version="1.0" encoding="UTF-8"?> <student/> 生成的一个xml文档: <?xml version="1.0" encoding="UTF-8"?> <student name="zhangsan"> <hello>hello Text</hello> <world>world text</world> </student> |
首先,待分析的文档如下:
1 2 3 4 5 6 7 8 9 | <?xml version="1.0" encoding="UTF-8"?> <students name="zhangsan"> <hello name="lisi">hello Text1</hello> <hello name="lisi2">hello Text2</hello> <hello name="lisi3">hello Text3</hello> <world name="wangwu">world text1</world> <world name="wangwu2">world text2</world> <world >world text3</world> </students> |
以下是代码实现
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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 Dom4JTest2 { public static void main(String[] args) throws Exception { SAXReader saxReader = new SAXReader(); // 获取文档对象 Document document = saxReader.read(new File("students.xml")); // 获取根元素 Element root = document.getRootElement(); System.out.println("Root: " + root.getName()); // 获取所有子元素 List<Element> childList = root.elements(); System.out.println("total child count: " + childList.size()); // 获取特定名称的子元素 List<Element> childList2 = root.elements("hello"); System.out.println("hello child: " + childList2.size()); // 获取名字为指定名称的第一个子元素 Element firstWorldElement = root.element("world"); // 输出其属性 System.out.println("first World Attr: " + firstWorldElement.attribute(0).getName() + "=" + firstWorldElement.attributeValue("name")); System.out.println("迭代输出-----------------------"); // 迭代输出 for (Iterator iter = root.elementIterator(); iter.hasNext();) { Element e = (Element) iter.next(); System.out.println(e.attributeValue("name")); } System.out.println("用DOMReader-----------------------"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); // 注意要用完整类名 org.w3c.dom.Document document2 = db.parse(new File("students.xml ")); DOMReader domReader = new DOMReader(); // 将JAXP的Document转换为dom4j的Document Document document3 = domReader.read(document2); Element rootElement = document3.getRootElement(); System.out.println("Root: " + rootElement.getName()); } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |