1

XML 文件:

<apps>
        <app name="google">
              <branches> 
                   <branch location="us">
                       <datacenters>
                               <datacenter city="Mayes County" />
                                <datacenter city="Douglas County" />
                       </datacenters>
                   </branch>
              </branches>
        </app>
        <app name="facebook">
        </app>
</apps>

Java 代码:

XPath xpath = XPathFactory.newInstance().newXPath();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
String expression = "/apps/app[name='google']";
Element element = xpath.evaluvate(expression,document.getDocumentElement();
Element app = (Element)xpath.evaluvate(expression,element,XPathConstants.NODE);

我可以存储 app 元素并在 app 元素中执行 xpath 表达式。像这样

xpath.evaluvate("branches/branch[@location='us']",app,XPathConstants.STRING);

如何在 VTD XML 中执行此操作?

4

1 回答 1

1

我认为您可以通过访问 VTD-XML 的 sourceforge 网站来学习 API。下面是我认为它的样子:

VTDGen vg = new VTDGen();
String filename ="this.xml";
if (vg.parseFile(filename, true)){
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("/apps/app[name='google']");
    int i=-1;
    while((i=ap.evalXPath())!=-1){
         //do something with i, it is the node index
    }


}
于 2012-12-19T04:51:39.383 回答