JAVA开发中解析XML文件常在框架中使用,而XML文件通常作为框架的配置文件供框架解析进而读取数据,因此掌握读取XML文件中数据是了解框架运行机制的基础。
解析XML有两种解析方式:
Dom:(适合修改xml)
将文档加载进内存,形成一颗dom树(document对象),将文档的各个组成部分封装为一些对象。(在内存中会形成dom树,写代码就非常方便,可以对dom树进行增删改查,但是dom树非常占内存,解析速度慢)
Sax:(适合读取xml)
逐行读取,读取到匹配的元素或节点后返回文档对象,相比于DOM,SAX可以在解析文档的任意时刻停止解析解析,基于事件驱动(不占内存,速度快,但是只能读取,不能回写,使用不方便)
XML解析器(DOM4J)民间组织开发的,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP,要使用Dom4J来解析xml要导入dom4j的jar包,读写XML文档主要依赖于org.dom4j.io包,有DOMReader和SAXReader两种方式,由于两种解析器的接口都是相同的,所以调用方式一致
1、使用Dom4j生成一个XML文件
/*** 使用dom4j生成xml文件*/@Testpublic void createXML() throws IOException {//创建一个空白的文档对象Document document = DocumentHelper.createDocument();//给文档对象创建一个根元素Element root = document.addElement("root");//给root添加两个个子元素Element childOne = root.addElement("childOne");Element childTwo = root.addElement("childTwo");//给这两个子元素添加属性childOne.addAttribute("id","childOneID");childOne.addAttribute("name","childOneName");childOne.addAttribute("class","childOneQualifiedName");childTwo.addAttribute("id","childTwoID");childTwo.addAttribute("name","childTwoName");childTwo.addAttribute("class","childTwoQualifiedName");//使用XMLWriter生成XML文件(OutputFormat.createPrettyPrint()格式化输出)XMLWriter xmlWriter = new XMLWriter(OutputFormat.createPrettyPrint());xmlWriter.setOutputStream(new FileOutputStream(new File("C:\\Users\\YanoHao\\Desktop\\aaa\\test.xml")));xmlWriter.write(document);}
生成的test.xml如下:
<?xml version="1.0" encoding="UTF-8"?><root><childOne id="childOneID" name="childOneName" class="childOneQualifiedName"/><childTwo id="childTwoID" name="childTwoName" class="childTwoQualifiedName"/>
</root>
2、使用SAX方式通过遍历节点读取XML文件内容
/*** 使用SAXReader解析器解析xml文件(Document----Element---Attribute)*/@Testpublic void parseXmlBySAXReader() throws DocumentException {//创建SAXReader解析器SAXReader saxReader = new SAXReader();//使用SAXReader解析器获取文档对象Document document = saxReader.read("C:\\Users\\YanoHao\\Desktop\\aaa\\test.xml");//获取dom对象的数据Element rootElement = document.getRootElement();System.out.println("根元素名称"+rootElement.getName()+"\n");List elements = rootElement.elements();for (Object e: elements) {Element element = (Element) e;System.out.println("子元素名称"+element.getName()+" ");List<Attribute> attributes = element.attributes();for (Attribute attribute :attributes) {System.out.println("子元素属性名"+attribute.getName()+" 值:"+attribute.getValue());}System.out.println();}}
3、结合XPath查询特定节点(“灵活简便”),注意使用需要导入dom4j子项目的jaxen…jar包
/*** 使用XPath查找xml中特定的节点(更多解析格式请参考文档)*/@Testpublic void parseXmlByXPath() throws DocumentException {/* 创建SAXReader解析器 */SAXReader saxReader = new SAXReader();//使用SAXReader解析器获取文档对象Document document = saxReader.read("C:\\Users\\YanoHao\\Desktop\\aaa\\test.xml");//使用XPath解析路径获取元素节点//1、选择AAA所有的BBB元素(xpath格式:/AAA/CCC)List<Element> list = document.selectNodes("/root/childTwo");for (Element e: list) {System.out.println(e.getName());}}