jdom xpath定位带xmlns命名空间的节点(转)
- jdom xpath定位带xmlns命名空间的节点
-
关键词:jdom xpath xmlns 命名空间 openjweb
在jdom中用 xpath定位节点通常采用以下方式:
XPath xpath=null;
Element anode = null;
SAXBuilder sb = new SAXBuilder();
Document doc = null;
try
{
doc = sb.build("xxx.xml");
}
catch(Exception ex)
{
return "解析xml失败!";
}xpath = XPath.newInstance("/节点1/节点2[@属性1='值1']");
anode=(Element)xpath.selectSingleNode(doc);但是在处理spring的bean文件时,发现这种方式定位不到想找的节点,下面是openjweb的core-service-demo.xml,现在要查找此文件里的hibernate配置,文件格式:
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
......<bean id="demosessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="demoDatasource" />
</property>
<property name="mappingResources">
<list>
<value>org/openjweb/core/entity/CommSubSystem.hbm.xml</value>......
</list></property>
</bean>
</beans>
现在要通过程序查找list节点,但是按照下面的方式发现取不到list的Element:xpath = XPath.newInstance("/beans/bean/property[@name='mappingResources']/list");
anode = (Element)xpath.selectSingleNode(doc );
后来从网上查找解决方案,发现是因为beans根节点带有xmlns命名空间。需要注册命名空间,见下面的代码:
xpath = XPath.newInstance("/ns:beans/ns:bean/ns:property[@name='mappingResources']/ns:list");
//ns是随便起的名字
xpath.addNamespace("ns","http://www.springframework.org/schema/beans");
anode = (Element)xpath.selectSingleNode(doc );上面定义了一个ns命名空间,另外在xpath的查找字符串中,每级节点都要增加 ns:,采用这种方式就可以查找list节点了。
jdom xpath定位带xmlns命名空间的节点(转)的更多相关文章
- 使用XPath查询带有命名空间(有xmlns)的XML(转)
使用XPath查询带有命名空间(有xmlns)的XML 标签: xmlsilverlightwebserviceencodingwpfinclude 2012-06-19 10:26 3235人阅读 ...
- (转载)JDOM/XPATH编程指南
JDOM/XPATH编程指南 本文分别介绍了 JDOM 和 XPATH,以及结合两者进行 XML 编程带来的好处. 前言 XML是一种优秀的数据打包和数据交换的形式,在当今XML大行于天下,如果没有听 ...
- 【转】wpf中的xmlns命名空间为什么是一个网址,代表了什么意思
wpf中的xmlns命名空间为什么是一个网址,代表了什么意思 http://blog.csdn.net/catshitone/article/details/71213371
- .NET(C#):使用XPath查询带有命名空间(有xmlns)的XML
原文http://www.cnblogs.com/mgen/archive/2011/05/24/2056025.html 众所周知,XmlDocument可以进行XPath查询,但实际上这里所说的X ...
- 使用jdom操作xml文件 去除子节点带有命名空间
package com.soft.common; import java.util.HashMap; import java.util.Map; import org.jdom2.Namespace; ...
- python 使用xpath解析含有命名空间(xmlns)的xml
解决办法: from lxml import etree xml = etree.parse("./cee.xml") root = xml.getroot() print(roo ...
- C#解析XML详解(XPath以及带命名空间NameSpace)
<?xml version="1.0" encoding="utf-8" ?> <bookstore> <book> < ...
- 移除“xmlns”命名空间
用XmlDocument创建一个文档,或者插入一个节点,默认会生成xmlns(命名空间)特性. 假定有一个xml文档如下结构: <?xml version="1.0" enc ...
- dom4j的xpath查找xml的指定节点
递归遍历所有节点http://blog.csdn.net/sidihuo/article/details/47318723 获取Document SAXReader saxReader = new S ...
随机推荐
- 安装Epson打印机但因lsb依赖错误而中断的驱动程序
sudo apt-get install printer-driver-escpr 安装Epson打印机但因lsb依赖错误而中断的驱动程序 问题: 我正在安装 Epson XP-310 驱动程序,从这 ...
- delphi 流程单打印
1.添加声明 f_count1: double; 2.得到拆分页数量 // Modified by 884 2018-04-20 14:50:18 AM0057 with aqTpCount do b ...
- thinkphp import标签
传统方式的导入外部JS和CSS文件的方法是直接在模板文件使用: 直线电机哪家好 <script type='text/javascript' src='/Public/Js/Util/Array ...
- Kubernetes的包管理工具Helm的安装和使用
1.源码安装 [root@master ~]# wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.0-linux-amd64 ...
- linux下怎么安装Go开发环境?linux部署golang
linux下怎么安装Go开发环境?linux部署golang 0.请自行安装SSH远程工具 1. SSH远程登录你的linux服务器 2. yum install mercurial安装 me ...
- SpringBoot-application:application.yml/配置文件详解
ylbtech-SpringBoot-application:application.yml/配置文件详解 springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优 ...
- AutoMapper Profile用法
using System; using System.Collections.Generic; using System.Linq; using System.Web; using AutoMappe ...
- Assert(断言) 的用法
Assert Assert是断言的意思,头文件为assert.h, assert是一个宏 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 在单元测试中经 ...
- JS动画完美框架
html部分 <!DOCTYPE html> <html lang="en"> <head> <link href="../cs ...
- 08_springboot2.x自定义starter
概述 starter:启动器 1.这个场景需要使用到的依赖是什么? 2.如何编写自动配置 规则: @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指 ...
我要投稿