How to ingore namespace prefixes on VTD Xpath lookup

[亡魂溺海] 提交于 2019-12-08 05:22:50

问题


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));
    }
}

回答1:


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!



来源:https://stackoverflow.com/questions/22360885/how-to-ingore-namespace-prefixes-on-vtd-xpath-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!