当xml带有命名空间时。dom4j的处理办法
2013-07-24 09:48
603 查看
转自:http://www.cnblogs.com/patrickchen/articles/1188920.html
当你解析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>
第一个方案.设置你的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());
}
}
第二个解决方案:设置你的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());
}
}
第三种方法:本人用的
,最笨也是最通用的方法,就是不使用开发环境给你提供的一系列对象,而是用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());
}
}
自己写的实例
cameraServerAddresses.xml
解析方法:
当你解析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>
第一个方案.设置你的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());
}
}
第二个解决方案:设置你的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());
}
}
第三种方法:本人用的
,最笨也是最通用的方法,就是不使用开发环境给你提供的一系列对象,而是用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());
}
}
自己写的实例
cameraServerAddresses.xml
<?xml version="1.0" encoding="UTF-8"?> <addresses xmlns="http://www.example.org/cameraServerAddress" > <address> <db>r1app.nvts.co</db> <zh>美国东1</zh> <en>US-East1</en> </address> <address> <db>r2app.nvts.co</db> <zh>日本1</zh> <en>JP-1</en> </address> <address> <db>r3app.nvts.co</db> <zh>欧洲1</zh> <en>EU-1</en> </address> </addresses>
解析方法:
/** * 初始化CameraServerAddress,从xml配置文件初始化 */ @SuppressWarnings("unchecked") public void initCameraServerAddresses(){ try { Map<String,String> uris = new HashMap<String, String>(); uris.put("cameraServerAddress" , "http://www.example.org/cameraServerAddress"); SAXReader reader = new SAXReader(); Document root = reader.read(this.getClass().getClassLoader().getResourceAsStream("cameraServerAddresses.xml")); XPath xpath = root.createXPath("//cameraServerAddress:address"); //创建XPath xpath.setNamespaceURIs(uris); //加入NameSpace List<DefaultElement> nodes = xpath.selectNodes(root); //执行搜索 for (DefaultElement de : nodes) { de.add(new Namespace("cameraServerAddress", "http://www.example.org/cameraServerAddress")); //这里也要再次加入NameSpace Node db = de.selectSingleNode("cameraServerAddress:db"); Node zh = de.selectSingleNode("cameraServerAddress:zh"); Node en = de.selectSingleNode("cameraServerAddress:en"); NVContext.cameraServerAddresses.add(new CameraServerAddress( db.getText(), zh.getText(), en.getText())); } } catch (Exception e) { log.error("初始化CameraServerAddress失败"); e.printStackTrace(); } }
相关文章推荐
- 当xml中存在命名空间,三种处理办法(dom4j)
- 当xml中存在命名空间,三种处理办法(dom4j)
- 当 xml中存在命名空间,三种处理办法(dom4j)
- 【转】+【改】DOM4J处理XML带有命名空间的四种方法
- 当xml中存在命名空间,三种处理办法(dom4j)
- DOM4J处理XML带有命名空间的四种方法
- java 在dom4j中,使用XPath处理XML(主要对命名空间的处理)
- xml内容解析,包含命名空间时需特殊处理(dom4j)
- 在jquery中处理带有命名空间的XML数据
- Dom4j解析带有命名空间的XML文件
- 在jquery中处理带有命名空间的XML数据
- dom4j创建带有命名空间的xml
- dom4j修改带有命名空间的xml文件
- dom4j处理带命名空间的XML-使用XPath
- XML中处理带有命名空间的XPath查询
- 在jquery中处理带有命名空间的XML数据
- Xpath dom4j解析带有命名空间的XML的方法
- 在jquery中处理带有命名空间的XML数据
- dom4j中通过xpath处理带命名空间的XML文件
- Dom4j解析带有命名空间的XML文件