读书人

DOM4J处置XML带有命名空间的四种方法

发布时间: 2012-11-03 10:57:43 作者: rapoo

DOM4J处理XML带有命名空间的四种方法
当你解析XML时,是否会因为命名空间的存在而不能得偿所愿呢?
.net上的解决方法我就不多说了(.net有世界上最详细的开发文档。这是我最欣赏微软的地方)
java方面,好多人推荐用dom4j处理xml,我也就说说在dom4j上处理带命名空间的xml
先说前两个方法,是从网上看来的。(来自http://blog.csdn.net/anyoneking/)摘抄如下:
xml代码example:

再说前三种方法,也是从网上看来的。http://www.cnblogs.com/patrickchen/articles/1188920.html

<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.15" id="1"> <list-property name="cssStyleSheets"> <structure> <property name="fileName">D: eport.cssproperty> structure> list-property> report>

第一个方案.设置你的xpath的命名空间setNamespaceURIs

public class TransferXML {public static void main(String[] args) throws Exception {SAXReader saxReader = new SAXReader();File file = new File("D:\test.xml");Document document = saxReader.read(file);Element root = document.getRootElement();Element ele = root.element("report");System.out.println(ele.getName());List list = document.selectNodes("//@name");System.out.println(list.size());}}

读书人网 >XML SOAP

热点推荐