VTD XML Internal XPath Expressions

I have an xml file like this one

<root>
 <test>
  <bla>test1</bla>
 </test>
 <test>
  <bla>test2</bla>
 </test>
 <test>
 </test>
</root>

Now I want to parse it using vtd-xml-parser using XPath expressions. I am looking for tags firsttest

VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("//test");

Now I want to search in this tag testfor tagsbla

int result = -1;
int count = 0;

while ((result = ap.evalXPath()) != -1) {
 // evaluate XPath Expressions within the test tags
}

Can someone tell me how to make this expression? I do not want to search the entire document for tags bla, because I want to be able to tag blatags test. I cannot do this if the tags are blaempty, and I am viewing the entire document for the tag bla.

+3
source share
4 answers

( ),

AutoPilot ap2 = new AutoPilot(); ap2.selectXPath("blah");

while ((result = ap.evalXPath()) != -1) {
 // evaluate XPath Expressions within the test tags
  int i2=-1;
  while((i2=ap2.evalXPath())!=-1){
     // do more stuff here
  }
}

catch , xpath xpath...

+3

, :

//test/bla

, - .

+2

2 xpaths. xpath, "test", , "bla".

VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("//test");

while (ap.evalXPath() != -1) {
    System.out.println("Inside Test tag");

    //now find the children called "bla"
    if(vn.toElement(VTDNav.FIRST_CHILD, "bla")){
    do{
        int val = vn.getText() ;
        if(val!=-1){
        String value = vn.toNormalizedString(val);
        System.out.println("\tFound bla: " + value);
        }
    }
    while(vn.toElement(VTDNav.NEXT_SIBLING, "bla"));
    }
    //move back to parent
    vn.toElement(VTDNav.PARENT);
}
+1

, VTD-XML, http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML, VTD.

I did not find this site on the home page, but it helps a lot at startup.

+1
source

Source: https://habr.com/ru/post/1723330/


All Articles