XPath表达式可能是您在这里需要的。您可以选择文档中的列节点,无论它们在何处使用此模式:
//column
这是一个工作片段:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(____your XML InputStream____);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//column");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
检查Java XPath API以获取更多信息。
您还应该检查 XML 输入的语法。matrix1 和 row1 没有结束标记,这是更正后的 XML:
<matrix1>
<row1>
<column>679</column>
<column>9</column>
</row1>
<row2>
<column>78</column>
<column>29</column>
</row2>
</matrix1>