I'm trying to parse the following XML and create Java objects corresponding to the main elements:
我正在尝试解析以下XML并创建与主要元素对应的Java对象:
<bookCase>
<material>wood</material>
<shelves>12</shelves>
...
<listOfBooks>
<book name="book1">
<author>someone</author>
<pages>200</pages>
...
</book>
<book name="book2">
<author>someone else</author>
<pages>500</pages>
...
</book>
</bookCase>
So I want to create Java objects BookCase, which contains a List of Book objects.
所以我想创建Java对象BookCase,它包含一个Book对象列表。
I'm trying to use AutoPilot with XPath, but although I can get to the main values (material="wood", shelves="12"), I don't know how to iterate through the listOfBooks and create the two Book objects. Any idea?
我正在尝试使用带XPath的AutoPilot,但是虽然我可以获得主要值(material =“wood”,shelf =“12”),但我不知道如何遍历listOfBooks并创建两个Book对象。任何想法?
With the simple method below I can fetch the index of the two Book elements, but what do I with them?
通过下面的简单方法,我可以获取两个Book元素的索引,但是我对它们有什么看法?
private List<Integer> getBooksNodes() throws VTDException {
final String xpath = "/bookCase/listOfBooks/book";
ap.selectXPath(xpath);
final List<Integer> nodeList = new ArrayList<>();
int node;
while ((node = ap.evalXPath()) != -1) nodeList.add(node);
ap.resetXPath();
return nodeList;
}
How do I tell the AutoPilot to explore the values for the XPath "author" and "pages" for each book individually so I can create a Book object each time AutoPilot has finished exploring that section?
如何告诉AutoPilot单独浏览每本书的XPath“作者”和“页面”的值,以便每次AutoPilot完成探索该部分时我都可以创建一个Book对象?
1 个解决方案
#1
0
Ok, below is the code for exploring the author and pages nodes... I am not making the assumption that the book node must have author or pages nodes...
好的,下面是探索作者和页面节点的代码......我没有假设书籍节点必须有作者或页面节点......
private List<Integer> getBooksNodes(VTDNav vn) throws VTDException {
final String xpath = "/bookCase/listOfBooks/book";
ap.selectXPath(xpath);
final List<Integer> nodeList = new ArrayList<>();
int node;
while ((node = ap.evalXPath()) != -1) {
nodeList.add(node);
// the logic that browses the pages and author nodes
// just print em out...
// remember vn is automatically moved to the xpath output by
// autoPilot
// we are gonna move the cursor manually now
if (vn.toElement(FIRST_CHILD,"author")){
int i = vn.getText();
if (i!=-1)
System.out.println(" author is ====>" + vn.toString(i));
vn.toElement(PARENT);
}
if (vn.toElement(FIRST_CHILD,"pages")){
int i = vn.getText();
if (i!=-1)
System.out.println(" author is ====>" + vn.toString(i));
vn.toElement(PARENT);
}
// also remember that in a xpath eval loop, if you are gonna
// move the cursor manually
// the node position going into the cursor logic must be identical
// to the node position existing the cursor logic
// an easy way to accomplish this is
// vn.push() ;
// your code that does manual node traversal
// vn.pop();
// but the logic above didn't use them
// because move cursor to child, then moving back, essentially move
// the cursor to the original position
}
ap.resetXPath();
return nodeList;
}
#1
0
Ok, below is the code for exploring the author and pages nodes... I am not making the assumption that the book node must have author or pages nodes...
好的,下面是探索作者和页面节点的代码......我没有假设书籍节点必须有作者或页面节点......
private List<Integer> getBooksNodes(VTDNav vn) throws VTDException {
final String xpath = "/bookCase/listOfBooks/book";
ap.selectXPath(xpath);
final List<Integer> nodeList = new ArrayList<>();
int node;
while ((node = ap.evalXPath()) != -1) {
nodeList.add(node);
// the logic that browses the pages and author nodes
// just print em out...
// remember vn is automatically moved to the xpath output by
// autoPilot
// we are gonna move the cursor manually now
if (vn.toElement(FIRST_CHILD,"author")){
int i = vn.getText();
if (i!=-1)
System.out.println(" author is ====>" + vn.toString(i));
vn.toElement(PARENT);
}
if (vn.toElement(FIRST_CHILD,"pages")){
int i = vn.getText();
if (i!=-1)
System.out.println(" author is ====>" + vn.toString(i));
vn.toElement(PARENT);
}
// also remember that in a xpath eval loop, if you are gonna
// move the cursor manually
// the node position going into the cursor logic must be identical
// to the node position existing the cursor logic
// an easy way to accomplish this is
// vn.push() ;
// your code that does manual node traversal
// vn.pop();
// but the logic above didn't use them
// because move cursor to child, then moving back, essentially move
// the cursor to the original position
}
ap.resetXPath();
return nodeList;
}