我有一个 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”等等,但我没有得到它,尝试了很多,请帮忙。
谢谢, 阿尔斯
最佳答案
您可以使用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/