在使用xml读写的过程中,用到了dom4j,也算是一个比较主流的xml包了,在使用的过程中,将学习经历记录一下,以后查阅也比较方便。
首先是在pom中添加依赖,在Maven的中心库搜索后选择了该包:
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6</version> </dependency>
添加完这个依赖后就可以使用了dom4j了,但是在使用的过程中发现了问题,在使用selectNodes函数时产生了异常。
后来上网查询发现还缺少了一些dom4j需要的包,于是还是在中心库中搜了搜,填了一堆进去。可能有多,不过总算能正常工作了。添加的依赖如下
<dependency> <groupId>jaxme</groupId> <artifactId>jaxme-api</artifactId> <version>0.3</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1-beta-4</version> </dependency> <dependency> <groupId>msv</groupId> <artifactId>xsdlib</artifactId> <version>20030807</version> </dependency> <dependency> <groupId>msv</groupId> <artifactId>relaxngDatatype</artifactId> <version>20030807</version> </dependency> <dependency> <groupId>pull-parser</groupId> <artifactId>pull-parser</artifactId> <version>2</version> </dependency> <dependency> <groupId>stax</groupId> <artifactId>stax</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>junitperf</groupId> <artifactId>junitperf</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>stax</groupId> <artifactId>stax-ri</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>clover</groupId> <artifactId>clover</artifactId> <version>1.3-rc4</version> </dependenc
在添加完dom4j依赖之后,我们来介绍一下常用的xml功能。
1.读取,利用SAXReader进行读取,然后进行后续的解析。
File file=new File(path); SAXReader sr=new SAXReader(); Document doc=sr.read(file); Element root=doc.getRootElement();
2.遍历所有节点
public void showElement(Element temp){ for (Iterator it=temp.elementIterator("groupId");it.hasNext();) { Element result=(Element)it.next(); if (result.elements().size()>0){ System.out.println(result.getName()); showElement(result); } else { System.out.println(result.getName()+" "+result.getStringValue()); } } }
3.取得当前节点的指定名称的子节点
public void selectNode(String target){ SAXReader saxReader = new SAXReader(); File file = new File("H:\m.xml"); Document document = null; try { document = saxReader.read(file); } catch (DocumentException e) { e.printStackTrace(); } Element met=root.element(target); System.out.println(met.getText()); }
4.利用selectNode以及selectNodes函数(对于有xmlns的需要特殊处理)
public void selectNode(String target){ Map map = new HashMap(); map.put("ns","http://maven.apache.org/POM/4.0.0"); SAXReader saxReader = new SAXReader(); File file = new File("H:\m.xml"); saxReader.getDocumentFactory().setXPathNamespaceURIs(map); Document document = null; try { document = saxReader.read(file); } catch (DocumentException e) { e.printStackTrace(); } List tmp = root.selectNodes("//ns:groupId");// 两个/指的是忽略当前节点位置在全文搜索,不加/则是当前节点的子节点中搜索,不再向深层搜索 System.out.println(tmp.size()); }
对于有命名空间的xml文件,必须采用上面的方法,使用selectNodes和selectSingleNode方法才能找到对应的Element,如果没有xmlns这种命名空间,只需要简单的直接使用即可。
5.遍历属性
public void attributeShow(Element root){ for (Iterator it=root.attributeIterator();it.hasNext();){ Attribute attribute=(Attribute)it.next(); System.out.println(attribute.getName()+":"+attribute.getText()); } }
6.对于读取xml文件,上面介绍了几种常用方法,下面介绍一下写xml的方法。首先是修改
public void addElement(Document doc){ Element root = doc.getRootElement(); Element added=root.addElement("element1"); added.setText("added value"); added.addAttribute("type","test"); for (Iterator it=root.attributeIterator();it.hasNext();){ Attribute atr=(Attribute)it.next(); atr.setName("change"); atr.setValue("change value"); } }
上面的例子添加了root的子节点element1 并为这个节点添加了属性和值,修改了root的属性(将所有属性改成一样的了,别在意,只是个例子)
那么接下来就是将改动保存到文件当中了
public void writeToFile(Document document){ try { FileWriter fw=new FileWriter(path); document.write(fw); fw.close(); } catch (IOException e) { e.printStackTrace(); } }
运行完毕后打开文件就可以看到改动已经被写入了文件。
dom4j提供了一个非常方便的类 DocumentHelper 这个类有很多的静态方法供我们调用,很方便
public void showDocumentHelper(){ Element root=document.getRootElement(); DocumentHelper.createAttribute(root,"attr1","value1"); Document newDoc=DocumentHelper.createDocument(); Element newElement=DocumentHelper.createElement("newElement"); XPath xPath=DocumentHelper.createXPath("path"); List listEle=DocumentHelper.selectNodes("path",root); }
还有将字符串转为xml以及xml转为字符串
Document document =DocumentHelper.parseText(str); String docStr=document.getStringValue(); String docStr=documnet.asXML();
再来介绍一下XmlWriter,就是可以写出比较易读的xml 将filewriter包装
XMLWriter xw=new XMLWriter(new FileWriter(new File(path)));