对于xml的解析和生成,我们在实际应用中用的比较多的是JDOM和DOM4J,下面通过例子来分析两者的区别(在这里我就不详细讲解怎么具体解析xml,如果对于xml的解析看不懂的可以先去看下我之前关于dom跟sax解析xml的随笔http://www.cnblogs.com/zhi-hao/p/3985720.html,其实理解了解析xml的原理,要想学习jdom跟dom4j就比较简单了,jdom跟dom4j只是基于底层api的更高级的封装,而dom和sax是解析xml的底层接口),我们在使用jdom和dom4j时要导入相应的包,这些包可以到jdom和dom4j的官网去下载。
首先通过jdom来生成xml
1 package xmlTest;
2 /**
3 * @author CIACs 4 */ 5 import java.io.FileOutputStream; 6 7 import org.jdom.Document; 8 import org.jdom.Element; 9 import org.jdom.output.Format; 10 import org.jdom.output.XMLOutputter; 11 12 public class Jdom { 13 14 public static void main(String[] args) throws Exception{ 15 Document doc = new Document(); 16 Element root = new Element("root"); 17 doc.addContent(root); 18 19 Element name = new Element("name"); 20 root.addContent(name); 21 root.setAttribute("author","CIACs").setAttribute("url", "http://www.cnblogs.com/zhi-hao/"); 22 name.addContent("CIACs"); 23 24 XMLOutputter out = new XMLOutputter(); 25 Format format = Format.getPrettyFormat(); 26 format.setIndent(" "); 27 out.setFormat(format); 28 out.output(doc,new FileOutputStream("jdom.xml")); 29 30 } 31 32 }
生成的xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <root author="CIACs" url="http://www.cnblogs.com/zhi-hao/"> 3 <name>CIACs</name> 4 </root>
这里生成的xml比较简单,这是为了容易理解,简单的理解了,复杂的其实也就会了。
接下来通过jdom对xml进行解析
1 package xmlTest;
2 /**
3 * @author CIACs 4 */ 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.util.List; 8 9 import org.jdom.Attribute; 10 import org.jdom.Document; 11 import org.jdom.Element; 12 import org.jdom.input.SAXBuilder; 13 import org.jdom.output.XMLOutputter; 14 15 public class Jdom2 { 16 17 public static void main(String[] args) throws Exception { 18 //通过SAXBuilder解析xml 19 SAXBuilder builder = new SAXBuilder(); 20 21 Document doc = builder.build(new File("jdom.xml")); 22 23 Element root = doc.getRootElement(); 24 25 System.out.println(root.getName()); 26 27 String name = root.getChild("name").getText(); 28 29 System.out.println("name: "+name); 30 31 List attrs = root.getAttributes(); 32 33 for(int i = 0; i < attrs.size();i++) 34 { 35 String attrName; 36 String attrValue; 37 Attribute attr = (Attribute)attrs.get(i); 38 attrName = attr.getName(); 39 attrValue = attr.getValue(); 40 System.out.println(attrName+":"+attrValue); 41 42 } 43 //删除属性url,并保存到jdom2.xml 44 root.removeAttribute("url"); 45 46 XMLOutputter out = new XMLOutputter(); 47 out.output(doc, new FileOutputStream("jdom2.xml")); 48 49 50 } 51 }
控制台窗口输出的结果:
写入jdom2.xml的内容
下面是用dom4j来操作xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package xmlTest;
2 /**
3 * @author CIACs 4 */ 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.util.List; 8 9 import javax.xml.parsers.DocumentBuilder; 10 import javax.xml.parsers.DocumentBuilderFactory; 11 12 import org.dom4j.Attribute; 13 import org.dom4j.Document; 14 import org.dom4j.DocumentFactory; 15 import org.dom4j.DocumentHelper; 16 import org.dom4j.Element; 17 import org.dom4j.io.DOMReader; 18 import org.dom4j.io.OutputFormat; 19 import org.dom4j.io.SAXReader; 20 import org.dom4j.io.XMLWriter; 21 22 public class Dom4j { 23 public static void main(String[] args) throws Exception { 24 //创建文档跟节点 25 Document doc = DocumentFactory.getInstance().createDocument(); 26 Element root = DocumentHelper.createElement("root"); 27 doc.setRootElement(root); 28 29 Element name = DocumentHelper.createElement("name"); 30 name.setText("CIACs"); 31 name.addAttribute("age", "22"); 32 root.add(name); 33 root.addElement("address").addAttribute("province", "guangdong").addElement("country").setText("guangzhou"); 34 35 //生成的xml输出到命令行窗口 36 OutputFormat format = new OutputFormat(" ",true); 37 XMLWriter writer = new XMLWriter(format); 38 writer.write(doc); 39 40 XMLWriter writer2 = new XMLWriter(new FileOutputStream(new File("dom4j.xml")),format); 41 //生成的xml写到内存中 42 writer2.write(doc); 43 44 45 System.out.println("-----------通过SAXReader来解析xml文档-----------"); 46 SAXReader reader = new SAXReader(); 47 Document doc2 = reader.read(new File("dom4j.xml")); 48 Element root2 = doc2.getRootElement(); 49 50 parse(root2); 51 52 System.out.println("---------通过DOMReader来解析xml文档-------------"); 53 54 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 55 DocumentBuilder db = dbf.newDocumentBuilder(); 56 //当一个程序中引用同名不同包的两个Document,其中一个必须要加包名加以区别 57 org.w3c.dom.Document doc3 = db.parse(new File("dom4j.xml")); 58 DOMReader domReader = new DOMReader(); 59 //将JAXP的document转换为dom4j的document 60 Document doc4 = domReader.read(doc3); 61 //获得根元素的名称 62 String rootName = doc4.getRootElement().getName(); 63 System.out.println(rootName); 64 65 } 66 67 public static void parse(Element element) 68 { 69 List<Attribute> list = element.attributes(); 70 if(!list.isEmpty()) 71 { 72 for(Attribute attr:list) 73 { 74 System.out.println("属性:"+attr.getName()+"="+attr.getText()); 75 } 76 } 77 List<Element> list2 = element.elements(); 78 for(int i=0;i<list2.size();i++) 79 { 80 81 82 83 if(list2.get(i).elements().size()>=1) 84 { 85 System.out.println(list2.get(i).getName()); 86 parse(list2.get(i)); 87 } 88 else 89 { 90 System.out.println(list2.get(i).getName()+": "+list2.get(i).getText()); 91 } 92 93 94 List<Attribute> list3 = list2.get(i).attributes(); 95 if(!list3.isEmpty()) 96 { 97 for(Attribute attr:list3) 98 { 99 System.out.println("属性:"+attr.getName()+"="+attr.getText()); 100 } 101 } 102 103 104 } 105 106 } 107