以下是 .xml 文件的片段。我做了以下事情:
Document doc = docBuilder.parse(filesInDirectory.get(i));
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr1 = xPath.compile("//codes[@ class ='class2']/code[@code]");
Object result1 = expr1.evaluate(doc, XPathConstants.NODESET);
NodeList nodes1 = (NodeList) result1;
现在,
System.out.println("result length"+":"+nodes1.getLength());
returns 2.
我想根据属性名称做出合乎逻辑的决定,例如(伪代码)
if(nodes1.contains(123))
或者
if(nodes1.contains(123) && nodes1.contains(456))
and make decision.
我该怎么做?
<metadata>
<codes class="class1">
<code code="ABC">
<detail "blah" "blah">
</code>
</codes>
<codes class="class2">
<code code="123">
<detail "blah blah"/>
</code>
<code code="456">
<detail "blah blah"/>
</code>
</codes>
</metadata>