[英]XPath NodeList order (Java)
我有以下XML結構。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<header>
<row>
<column n="name" />
<column n="age" />
<column n="email" />
</row>
</header>
<body>
<row>
<column>Foo</column>
<column>99</column>
<column>foo@test.com</column>
</row>
</body>
</root>
我正在使用以下XPath表達式來獲取標題列,同樣獲取正文列。
PathExpression headerExpr = xPath.compile("/root/header/row/column/@n");
NodeList headerColumns = (NodeList) headerExpr.evaluate(document, XPathConstants.NODESET);
PathExpression bodyExpr = xPath.compile("/root/body/row/column");
NodeList bodyColumns = (NodeList) bodyExpr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < headerColumns.getLength(); i++) {
System.out.println((Attr) headerColumns .item(i)).getValue() + ": " + bodyColumns.item(i).getTextContent());
}
我可以假設XPath將始終以相同的順序返回標題和正文列,以便它們始終匹配嗎? 或者我甚至可以假設列始終按文檔順序返回?
使用XPath時始終保留文檔順序,這里有一些官方供您參考: http : //msdn.microsoft.com/en-us/library/ms256090.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.