质检报告哪里办多少钱:用dom4j对带有命名空间的xml文件使用xpath取值
来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 21:33:32
关于用dom4j对带有命名空间的xml文件使用xpath取值的例子
//设置http://www.test.com/dataspec/映身名为xmlns
Map map = new HashMap();
map.put("xmlns", "http://www.test.com/dataspec/"); 当然map的key值可以指定为任何值,不一定得为"xmlns"只要把xpath中的值修改为你定义的值即可
//下面是取rqQ结点值
XPath xPath = this.getDocument().createXPath(
"//xmlns:head//xmlns:publicHead//xmlns:sssq//xmlns:rqQ"); 如果上面map中key值不为"xmlns",为"aa"则该语句可写为XPath xPath = this.getDocument().createXPath(
"//aa:head//aa:publicHead//aa:sssq//aa:rqQ");
xPath.setNamespaceURIs( map );
String startDate = xPath.selectSingleNode( document ).getText();
//下面是取rqZ结点值
xPath = this.getDocument().createXPath(
"//xmlns:head//xmlns:publicHead//xmlns:sssq//xmlns:rqZ");
xPath.setNamespaceURIs( map );
String endDate = xPath.selectSingleNode( document ).getText();
另外不用xpath的时候,可以给SaxReader的DocumentFactory设定命名空间map此时便可以用document的selectNodes等方法了如:saxReader.getDocumentFactory().setXPathNamespaceURIs(map);document.selectSingleNode("//xmlns:head//xmlns:publicHead//xmlns:sssq//xmlns:rqZ");
xml文件如下:
java代码如下:
Document document = new SAXReader().read( new File( filename ) ); //filename为文件名//设置http://www.test.com/dataspec/映身名为xmlns
Map map = new HashMap();
map.put("xmlns", "http://www.test.com/dataspec/"); 当然map的key值可以指定为任何值,不一定得为"xmlns"只要把xpath中的值修改为你定义的值即可
//下面是取rqQ结点值
XPath xPath = this.getDocument().createXPath(
"//xmlns:head//xmlns:publicHead//xmlns:sssq//xmlns:rqQ"); 如果上面map中key值不为"xmlns",为"aa"则该语句可写为XPath xPath = this.getDocument().createXPath(
"//aa:head//aa:publicHead//aa:sssq//aa:rqQ");
xPath.setNamespaceURIs( map );
String startDate = xPath.selectSingleNode( document ).getText();
//下面是取rqZ结点值
xPath = this.getDocument().createXPath(
"//xmlns:head//xmlns:publicHead//xmlns:sssq//xmlns:rqZ");
xPath.setNamespaceURIs( map );
String endDate = xPath.selectSingleNode( document ).getText();
另外不用xpath的时候,可以给SaxReader的DocumentFactory设定命名空间map此时便可以用document的selectNodes等方法了如:saxReader.getDocumentFactory().setXPathNamespaceURIs(map);document.selectSingleNode("//xmlns:head//xmlns:publicHead//xmlns:sssq//xmlns:rqZ");
用dom4j对带有命名空间的xml文件使用xpath取值
Dom4j中XPath相对路径的使用
使用 dom4j 解析 XML
使用 dom4j 解析 XML
Dom4j下载及使用Dom4j读写XML简介
Dom4j的使用
使用JAXP处理XML文件
Java Web Project使用DOM4J中关于获取资源文件的注意
Dom4j的使用(全而好的文章)
Dom4j 使用简介 - ShaHuShaBoJue的专栏 - CSDN博客
.NET生成Rss的xml文件
web.xml 文件简介
传智播客:xml入门以及解析xml文件 - habernate的日志 - 网易博客
避免 XPath 注入的危险
【女人空间】平进能使自己带有一点女人的矜持
Setting Doctype in an XML file (xml文件中加载xml文件失败解决方法)
浅谈对取名字用简体字、繁体字的看法
[原创]如何恢复显示U盘里被隐藏的文件(U盘里没有文件却提示已使用很大的空间?) - 飞墨...
ASP.NET中XML文件操作篇
带有数字的成语
带有数字的成语
带有反义词的成语
带有刻度的安全套
带有数字的成语