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 ...
随机推荐
- 人脸识别-常用的数据库Face Databases From Other Research Groups
Static/Videos Static Single/Multiple faces Single? Gray/Color Color Resolution Vaious Face pose Fron ...
- 怎样查询Vultr当月账单和历史账单
关于Vultr账单的问题,Vultr是可以查看当月的实时账单,也可以查看历史账单,下面就教大家查看账单的方法. 一.查看Vultr当月账单 首先我们要进入Vultr官网,登录自己的Vultr账号,登录 ...
- Android Studio 配置快速生成模板代码
前言 Android studio 有提供快速生成模板代码的功能,其实这个功能也可以自定义配置.此篇博客将讲解如何使用此功能 进入Settings 选择 Editor > Live Templa ...
- Binary XML file line #23: Error inflating class android.widget.TextView
分析一波,报错23行TextView的问题,但是检查了xml没有发现23行又TextView相关代码,就不应该继续纠结xml了,代码是通过R文件拿到xml资源的,你就应该怀疑是R文件的问题,R文件编译 ...
- C++Builder常用函数
BCB函数集 1.内存分配 函数名称 AllocMem 函数说明 在队中分配指定字节的内存块,并将分配的每一个字节初始化为 0.函数原型如下: void * __fastcall AllocM ...
- LUOGU P4253 [SCOI2015]小凸玩密室(树形dp)
传送门 解题思路 玄学树形\(dp\),题目描述极其混乱...看错了两次题,设首先根据每次必须点完子树里的灯才能点别的,那么点灯情况只有两种,第一种是点到某一个祖先,第二种是点到某一个祖先的兄弟.所以 ...
- CCPC 2019 网络赛 1006 Shuffle Card
// 签到题,比赛时候写双向链表debug了半天,发现有更好方法,记录一下. Shuffle Card HDU 6707 题意: 有一 \(n\) 张卡片,编号 \(1~n\) ,给定初始编号 ...
- vue之样式问题
在样式开发过程中,有两个问题比较突出: 全局污染 —— CSS 文件中的选择器是全局生效的,不同文件中的同名选择器,根据 build 后生成文件中的先后顺序,后面的样式会将前面的覆盖: 选择器复杂 — ...
- USACO training course Number Triangles 数塔 /// DP oj10122
题目大意: ...就是数塔 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 7+3+8+7+5=30 ...
- SpringCloud学习笔记《---03 Ribbon Rule---》核心篇