3

I have a xml file for example like this

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

Now I want to parse it with the vtd-xml-parser by using XPath expressions. First I search for the test tags by

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

Now I want to search within this test tags for bla tags

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 expressions? I don't want to search the entire document for bla tags as I want to be able to assign the bla tags to the test tags. I cannot do this if the bla tags are empty for example and I search the entire document for the bla tag.

halloei
  • 1,892
  • 1
  • 25
  • 45
Sebastian Müller
  • 5,471
  • 13
  • 51
  • 79

4 Answers4

3

You can declare another autopilot (shown below), although it is not always the simplest way

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

then nest that in the loop

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

But the catch is that the second xpath needs to be relative xpath expression...

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
  • will this approach improve any performance in terms o cpu cycles? – Selvakumar Esra Jun 21 '12 at 03:39
  • hi, dont forget to use nv.push and nv.pop to keep up with the current cursor position, there is a example here http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML – Nei Jan 01 '13 at 19:28
2

The initial paragraphs seem to indicate that you want this:

//test/bla

However, the ending paragraph seems to indicate that you want something different.

kdgregory
  • 38,754
  • 10
  • 77
  • 102
1

For people who is learning VTD-XML there is a excellent how to programming article here http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML wrote by the VTD developers itself.

Didn't find this one there homepage, but it help a lot to start up.

Nei
  • 997
  • 1
  • 10
  • 9
1

Here is an alternative to using 2 xpaths. Use just one xpath to get the "test" tags, then use a loop to iterate over its children looking for "bla" tags.

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);
}
Xelian
  • 16,680
  • 25
  • 99
  • 152
dogbane
  • 266,786
  • 75
  • 396
  • 414