给定以下 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stock SYSTEM "new.dtd">
<stock>
<book num="myBook1">
<title>Lindsy Boxer</title>
<author>James Patterson</author>
<publisher>LittleBig</publisher>
<price>18.21</price>
<chapter>
<title>Alex Cross Is Back - Chapter A</title>
<paragraph>
This is the <emph>first</emph> paragraph.
<image file="alexCrossImage.gif"/>
afetr image...
</paragraph>
<paragraph>
This is the <emph>second</emph> paragraph.
<image file="alexCrossImageAnother.gif"/>
afetr image...
</paragraph>
</chapter>
<chapter>
<title>Along Came A Spider - Chapter B</title>
<section>
<title>Along Came A Spider - Chapter B - section 1</title>
<paragraph>
This is the <emph>first</emph>paragraph for chapter TWO section ONE.
<image file="Chapter_B_firstParagraphImage.gif"/>
afetr image...
</paragraph>
<paragraph>
This is the <emph>second</emph> paragraph for chapter TWO section ONE.
<image file="Chapter_B_secondParagraphImage.gif"/>
afetr image...
</paragraph>
</section>
</chapter>
<chapter>
<title>Chapter C</title>
<paragraph>
This chapter has no images and only one paragraph
</paragraph>
</chapter>
</book>
<book num="myBook2">
<title>Jack Reacher Series</title>
<author>Lee Child</author>
<author>Jonny White</author>
<publisher>Pocket</publisher>
<price>5.99</price>
<chapter>
<title>Jack Reacher - Chapter ONE</title>
</chapter>
<chapter>
<title>Jack Reacher - Chapter TWO</title>
<paragraph>
This is the <emph>second</emph> paragraph of SECOND book chapter TWO.
<image file="Jack_Reacher_Picture_Top_Sniper_US_Army.gif"/>
afetr image...
</paragraph>
</chapter>
</book>
<book num="myBook3">
<title>Alex Cross - Double Cross</title>
<author>James Patterson</author>
<publisher>Spectra</publisher>
<price>17.30</price>
<chapter>
<title>Alex Cross - Double Cross - Chapter A</title>
</chapter>
</book>
</stock>
当我输入以下查询时:
Object myQuery= xpath.evaluate("stock/book/chapter[3]/preceding-sibling::chapter//title",doc,XPathConstants.NODESET);
我得到的输出为:
<title>Alex Cross Is Back - Chapter A</title>
<title>Along Came A Spider - Chapter B</title>
<title>Along Came A Spider - Chapter B - section 1</title>
但我想要的是当我的 XPath 使用前兄弟轴时,我想以相反的顺序返回节点,即:
* <title>Along Came A Spider - Chapter B</title>
* <title>Along Came A Spider - Chapter B - section 1</title>
* <title>Alex Cross Is Back - Chapter A</title>
所以基本上,如您所见,Java 以混合顺序返回节点,按 XML 文件中节点的顺序,但我需要的是,当 XPath 使用前兄弟轴时,然后反向返回节点命令 。
这不是发生这种“行为”的唯一命令(返回混合节点)。
我的目标是解决这个问题,我试图改变XPath's
evalute
,但它仍然不起作用。
谁能给出一些指示/提示如何做到这一点?
附言
使用这段代码:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("new.xml");
//create an XPath factory
XPathFactory factory = XPathFactory.newInstance();
//create an XPath Object
XPath xpath = factory.newXPath();
//***********************************************************//
Object myQuery= xpath.evaluate("stock/book/chapter[3]/preceding-sibling::chapter//title",doc,XPathConstants.NODESET);
System.out.println("The Results are:");
NodeList nodes = (NodeList) myQuery;
//print the output
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("i: " + i);
System.out.println("*******");
System.out.println(nodeToString(nodes.item(i)));
System.out.println("*******");
}
我得到了上述结果(混合节点)