当 xml中存在命名空间,三种处理办法(dom4j)
来源:互联网 发布:mac 全角半角 编辑:程序博客网 时间:2024/06/15 12:18
当你解析XML时,是否会因为命名空间的存在而不能得偿所愿呢?
.net上的解决方法我就不多说了(.net有世界上最详细的开发文档。这是我最欣赏微软的地方)
java方面,好多人推荐用dom4j处理xml,我也就说说在dom4j上处理带命名空间的xml
先说前两个方法,是从网上看来的。(来自http://blog.csdn.net/anyoneking/)摘抄如下:
xml代码example:
<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.css</property>
</structure>
</list-property>
</report>
<list-property name="cssStyleSheets">
<structure>
<property name="fileName">D: eport.css</property>
</structure>
</list-property>
</report>
第一个方案.设置你的xpath的命名空间setNamespaceURIs
public class TransferXML {
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design");
SAXReader saxReader = new SAXReader();
File file = new File("D:/test.xml");
Document document = saxReader.read(file);
XPath x = document.createXPath("//design:list-property");
x.setNamespaceURIs(map);
List nodelist = x.selectNodes(document);
System.out.println(nodelist.size());
}
}
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design");
SAXReader saxReader = new SAXReader();
File file = new File("D:/test.xml");
Document document = saxReader.read(file);
XPath x = document.createXPath("//design:list-property");
x.setNamespaceURIs(map);
List nodelist = x.selectNodes(document);
System.out.println(nodelist.size());
}
}
第二个解决方案:设置你的DocumentFactory()的命名空间 setXPathNamespaceURIs
public class TransferXML {
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design");
SAXReader saxReader = new SAXReader();
File file = new File("D:/test.xml");
saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
Document document = saxReader.read(file);
List tmp = document.selectNodes("//design:list-property");
System.out.println(tmp.size());
}
}
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design");
SAXReader saxReader = new SAXReader();
File file = new File("D:/test.xml");
saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
Document document = saxReader.read(file);
List tmp = document.selectNodes("//design:list-property");
System.out.println(tmp.size());
}
}
第三种方法:本人用的,最笨也是最通用的方法,就是不使用开发环境给你提供的一系列对象,而是用XPath语法中自带的local-name() 和 namespace-uri() 指定你要使用的节点名和命名空间。
当你遇到使用xslt来样式化xml时,就知道这个笨方法的好处了:
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);
List tmp = document.selectNodes("//*[local-name()='report' and namespace-uri()='http://www.eclipse.org/birt/2005/design']/* [local-name()='list-property']");
System.out.println(tmp.size());
}
}
public static void main(String[] args) throws Exception
SAXReader saxReader = new SAXReader();
File file = new File("D:/test.xml");
Document document = saxReader.read(file);
List tmp = document.selectNodes("//*[local-name()='report' and namespace-uri()='http://www.eclipse.org/birt/2005/design']/* [local-name()='list-property']");
System.out.println(tmp.size());
}
}
- 当xml中存在命名空间,三种处理办法(dom4j)
- 当 xml中存在命名空间,三种处理办法(dom4j)
- dom4j解析xml基本方法以及当xml中存在命名空间时的三种处理办法
- 当xml带有命名空间时。dom4j的处理办法
- 当xml中存在命名空间,三种处理办…
- DOM4J处理XML带有命名空间的四种方法
- dom4j中通过xpath处理带命名空间的XML文件
- dom4j中通过xpath处理带命名空间的XML文件
- dom4j解析xml文件命名空间xmns的处理
- dom4j处理带命名空间的XML-使用XPath
- 【转】+【改】DOM4J处理XML带有命名空间的四种方法
- xml内容解析,包含命名空间时需特殊处理(dom4j)
- dom4j 两种创建XML Document方法中命名空间的差异!
- xml处理命名空间
- dom4j 解析带命名空间的XML
- dom4j创建带有命名空间的xml
- dom4j(5) 编码 命名空间处理
- dom4j 存在命名空间导致document.selectNodes() 无返回结果
- Linux 杂记(一)
- 一个方便有用的Delphi数据库操作类(转)
- 该如何加载google-analytics(或其他第三方)的JS (转自http://www.cnblogs.com/BearsTaR/archive/2010/05/12/best_use_google_analytics.html)
- 支持UCenter的PHP建站程序一览
- 噬石真象
- 当 xml中存在命名空间,三种处理办法(dom4j)
- MS sql两个不同数据库之间 进行数据倒换
- 用 按钮实现 IFRAME的跳转
- 国内PHP开源建站程序一览
- 动态库中创建对话框
- 顺手记点LINUX命令
- CTE学习
- Reuse-Based Software Engineering : Techniques, Organization, and controls
- 一个C#数组中 特殊项的选取