我有一个像这样的xml:
<SkillMap>
<ExitPoint ID="01">
<NodeName>abcd</NodeName>
</ExitPoint>
<ExitPoint ID="04">
<NodeName>defg</NodeName>
</ExitPoint>
<ExitPoint ID="22">
<NodeName>mnop</NodeName>
</ExitPoint>
</SkillMap>
我试图基于ID值获取
<ExitPoint>
节点的值,即如果我输入ID为01,则应给出“ abcd”,如果输入22,则应给出“ mnop”,依此类推,但是我不是得到它,尝试了很多,请帮助。谢谢,
Ars
最佳答案
您可以使用Xpath进行操作,请考虑以下来自JAXP Specification 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);
关于java - 根据父属性值获取xml节点子元素值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10953188/