简体   繁体   English

XPath NodeList顺序(Java)

[英]XPath NodeList order (Java)

I have the following XML structure. 我有以下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>

I'm using the following XPath expression to get the header columns and likewise to get the body columns. 我正在使用以下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());
        }

Can I assume that XPath will always return the header and body columns in the same order so that they will always match? 我可以假设XPath将始终以相同的顺序返回标题和正文列,以便它们始终匹配吗? Or can I even assume that the columns are always returned in document order? 或者我甚至可以假设列始终按文档顺序返回?

使用XPath时始终保留文档顺序,这里有一些官方供您参考: http//msdn.microsoft.com/en-us/library/ms256090.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM