I will use the following XML document in the examples below.
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <author>Doe, John</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> </book> <book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-11-17</publish_date> </book> </catalog>Loading the XML document:
The following code parses the XML file and creates the navigator and autopilot objects.
final VTDGen vg = new VTDGen();
vg.parseFile("books.xml", false);
final VTDNav vn = vg.getNav();
final AutoPilot ap = new AutoPilot(vn);
Selecting all titles:Print out all the title nodes using an XPath expression of
/catalog/book/title. First call selectXPath to compile the expression and then use evalXPath to move the cursor to the selected nodes in the result.
ap.selectXPath("/catalog/book/title");
while (ap.evalXPath() != -1) {
int val = vn.getText();
if (val != -1) {
String title = vn.toNormalizedString(val);
System.out.println(title);
}
}
Selecting all book ids and authors:This one is a bit more involved as a book can have many authors. In the code below, I first run an XPath to select the books and then iterate over the children, selecting the author nodes.
ap.selectXPath("/catalog/book");
while (ap.evalXPath() != -1) {
int val = vn.getAttrVal("id");
if(val != -1){
String id = vn.toNormalizedString(val);
System.out.println("Book id: " + id);
}
if(vn.toElement(VTDNav.FIRST_CHILD,"author")){
do{
val = vn.getText();
if(val != -1){
String author = vn.toNormalizedString(val);
System.out.println("\tAuthor:" + author);
}
}while(vn.toElement(VTDNav.NEXT_SIBLING,"author"));
}
vn.toElement(VTDNav.PARENT);
}
The output is:
Book id: bk101 Author:Gambardella, Matthew Author:Doe, John Book id: bk102 Author:Ralls, Kim Book id: bk103 Author:Corets, EvaRelated Posts:
Using XPath with DOM
Could you please let me know whether it is free software or licensed one?
ReplyDeleteGoogle it
DeleteI think if vn.toElement(VTDNav.FIRST_CHILD,"author") fails, according to the documentation of "toElement()", there's no position change, so we should put "vn.toElement(VTDNav.PARENT)" inside the "if"
ReplyDelete