VTD-XML: Only closing tag check is not working

239 views Asked by At

I'm Using VTD-XML parser to get the value from XML with path given. Below my java code.

VTDGen vg = new VTDGen();
vg.setDoc(xml.getBytes());
vg.parse(true);
VTDNav vn = vtdGen.getNav();
AutoPilot ap = new AutoPilot(vn);
xpath="ROOT/STUDENT[(not(DATE) or DATE='')]/NAME";
ap.selectXPath(xpath);

while(ap.evalXPath() != -1) {
        long l = vn.getContentFragment();
        value= vn.toString((int )l, (int)(l>>32));
}

And my xml look like this.

<ROOT>
<STUDENT>
    <NAME>John</NAME>
    <DATE>12-JUNE-18</DATE>
</STUDENT>
<STUDENT>
    <NAME>Peter</NAME>
    <DATE/>
</STUDENT>
</ROOT>

I wanted to select the name peter using the XPATH. This xpath is working in xsl transformation, but is not working in VTD-XML.

Am i doing anything wrong with my parsing or XPATH?

1

There are 1 answers

4
vtd-xml-author On

When you finished parsing xml with VTD-XML, the default cursor position for the VTDNav is at the root element node. In your case, it is at ROOT node. So when you run the xpath "ROOT/STUDENT[(not(DATE) or DATE='')]/NAME", you are actually querying /ROOT/ROOT/STUDENT/NAME node.

The easiest correction is to change XPath to STUDENT[(not(DATE) or DATE='')]/NAME, which is a relative xpath.

Or you can use an absolute xpath /ROOT/STUDENT[(not(DATE) or DATE='')]/NAME.

Or you can first call VTDNav's toElement() method and give it a "PARENT" parameter to move the cursor to the document node... it will work too

After a few testing on my end, I discover that this is a bug in a method inside VTDNav class called

XPathStringVal_Matches(int j, String s);

Will you be comfortable with cutting/pasting some code to rectify the bug?

I will provide you with instruction, very simple to fix...