I have a bit complex XML.
<?xml version="1.0" encoding="UTF-8"?><test:mark><parent parent_id="1"><child child_id="1_1">11Value</child><child child_id="1_2">12Value</child><child child_id="1_3">13Value</child></parent><other other_id="1"><inner>1233</inner></other></test:mark>
I am very new to VTD and now instead of parsing entire XML by DOM or SAX I want to get the value of <child child_id="1_2">
Can anyone suggest what XPATH expression can be used. Thanks in advance for the help. Other better options are welcome as well.
Below is what I am doing. And is not working!!
VTDGen vtdGen = new VTDGen();
vtdGen.parseFile("E:/JavaProjects/SampleTestingFiles/SampleXML.xml", true);
VTDNav vtdNav = vtdGen.getNav();
AutoPilot autoPilot = new AutoPilot(vtdNav);
try {
autoPilot.selectXPath("//parent/child[@child_id='1_2']");
if(autoPilot.evalXPathToBoolean()) {
int token = autoPilot.evalXPath();
if(token != -1) {
String value = vtdNav.toNormalizedString(token);
System.out.println("The value is - " + value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
If I do something like autoPilot.selectXPath("//parent/child[@child_id]");
. It selects some of the tags with attribute @child_id
but not all.
Any help is much appreciated even the documentation on how we should give XPATHs can help.
Your XPath evaluation logic not correct (your xpath expression seems ok)...
if(autoPilot.evalXPathToBoolean()) {
int token = autoPilot.evalXPath();
if(token != -1) {
String value = vtdNav.toNormalizedString(token);
System.out.println("The value is - " + value);
}
}
should be rewritten as
int i=-1;
while((i=autoPilot.evalXPath())!=-1){
String value = vtdNav.toNormalizedString(i);
System.out.println("The value is - " + value);
}