XPath NodeList顺序(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

点赞