0

我有一个像这样的 xml:-

 <SkillMap>
   <ExitPoint ID="01">
    <NodeName>abcd</NodeName>
   </ExitPoint>
   <ExitPoint ID="04">
    <NodeName>defg</NodeName>
   </ExitPoint>
   <ExitPoint ID="22">
    <NodeName>mnop</NodeName>
   </ExitPoint>
  </SkillMap>

我试图<ExitPoint>根据 ID 值获取节点的值,即如果我输入 ID 为 01,它应该给出“abcd”,如果 22 它应该给出“mnop”等等,但我没有得到它,尝试了很多,请帮助。

谢谢,阿斯

4

1 回答 1

1

您可以使用Xpath执行此操作,请考虑取自JAXP 规范 1.4的以下示例(我建议您对此进行咨询):

// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.Document document = builder.parse(new File("/widgets.xml"));
// evaluate the XPath expression against the Document
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget[@name='a']/@quantity";
Double quantity = (Double) xpath.evaluate(expression, document, XPathConstants.NUMBER);
于 2012-06-08T17:17:55.150 回答