问题描述
我正在构建一个基于VTD的XML解析引擎,以便处理来自多个输入系统的文件。
我正在尝试从带有命名空间的标签中获取值前缀:
<?xml version =1.0encoding =UTF-8?>
< cli:clients xmlns declarations>
< cli:client>
< dat:name>客户名称< / dat:name>
< dat:age> 1< / dat:age>
< / cli:client>
并查询以下X路径:
// client / age / text()
// client / name / text()
如何设置VTD AutoPilot忽略命名空间前缀?
注意:我无法更改xpath,因为我已经使用JDK的默认xpath引擎实现了这个引擎。
UPDATE :请参阅下面我用来测试的代码。文件类似于顶部的文件:
@Test
public void doFile()抛出异常{
byte [] xmlData = FileUtils.loadFile(namespace-test.xml);
VTDGen gen = new VTDGen();
gen.setDoc(xmlData);
gen.parse(false);
VTDNav vtd = gen.getNav();
AutoPilot pilot = new AutoPilot(vtd);
pilot.selectXPath(// clients);
int re = pilot.evalXPath();
System.out.println(re);
if(re> = 0){
System.out.println(vtd.toString(re));
}
}
As根据@ vtd-xml-author评论,我得到了最新版本的VTDNav.java文件,并在我自己的项目中编译。
该解决方案首次尝试! / p>
I'm building a VTD based XML Parsing engine in order to process files from several input systems.
I'm currently trying to get values from tags with namespace prefix:
<?xml version="1.0" encoding="UTF-8"?>
<cli:clients xmlns declarations >
<cli:client>
<dat:name>CLIENT NAME</dat:name>
<dat:age>1</dat:age>
</cli:client>
and querying the following Xpaths:
//client/age/text()
//client/name/text()
How can I set VTD AutoPilot to ignore the namespace prefix?
NOTE: I cannot change the xpaths as I already have this engine in production implemented with JDK's default xpath engine.
UPDATE: See below the code I am using to test. File is similar to the one on the top:
@Test
public void doFile() throws Exception {
byte[] xmlData = FileUtils.loadFile("namespace-test.xml");
VTDGen gen = new VTDGen();
gen.setDoc(xmlData);
gen.parse(false);
VTDNav vtd = gen.getNav();
AutoPilot pilot = new AutoPilot(vtd);
pilot.selectXPath("//clients");
int re = pilot.evalXPath();
System.out.println(re);
if (re >= 0) {
System.out.println(vtd.toString(re));
}
}
As per @vtd-xml-author comments, I got the newest version of VTDNav.java file and compiled on my own project.
The solution worked on the first try!
这篇关于如何在VTD Xpath查找上输入名称空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!