英文:
java xpath expression get only value if child node is different
问题
我想要获取一个特定节点的值,如果子节点不同于我表达式中的内容:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File("test.xml"));
List<String> data = new ArrayList<>();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate("/name1/type/*[name()!= 'pmc']", document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Element el = (Element) nodes.item(i);
data.add(el.getAttribute("id"));
}
System.out.println(data);
test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<name1>
<type id ="1">
<coord>67</coord>
<lmc>57657</lmc>
</type>
<lang>
<eng>989</eng>
<spa>123</spa>
</lang>
</name1>
<name2>
<type id ="2">
<coord>534</coord>
<lmc>654654</lmc>
</type>
<lang>
<eng>354</eng>
<spa>2424</spa>
</lang>
</name2>
<name3>
<type>
<coord>23432</coord>
<pmc>14324</pmc>
</type>
<lang>
<eng>141</eng>
<spa>142</spa>
</lang>
</name3>
例如,我只想获取具有子节点 pmc
而不是其他节点的 id
值。
英文:
I woudlike to get one specific value of node if child node is different like present in my expression :
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File("test.xml"));
List<String> data = new ArrayList<>();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate("/name1/type/*[name()!= 'pmc']", document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Element el = (Element) nodes.item(i);
data.add(el.getAttribute("id"));
}
System.out.println(data);
test.xml :
<?xml version="1.0" encoding="UTF-8"?>
<name1>
<type id ="1">
<coord>67</coord>
<lmc>57657</lmc>
</type>
<lang>
<eng>989</eng>
<spa>123</spa>
</lang>
</name1>
<name2>
<type id ="2">
<coord>534</coord>
<lmc>654654</lmc>
</type>
<lang>
<eng>354</eng>
<spa>2424</spa>
</lang>
</name2>
<name3>
<type>
<coord>23432</coord>
<pmc>14324</pmc>
</type>
<lang>
<eng>141</eng>
<spa>142</spa>
</lang>
</name3>
For example I wouldlike to get only the id
if the childnode is 'pmc'
and not the others.
答案1
得分: 1
因为问题并不是十分清楚,我们试着这样来处理。
假设您的 XML 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<name1>
<type id ="1">
<coord>67</coord>
<lmc>57657</lmc>
</type>
<lang>
<eng>989</eng>
<spa>123</spa>
</lang>
</name1>
<name2>
<type id ="2">
<coord>534</coord>
<lmc>654654</lmc>
</type>
<lang>
<eng>354</eng>
<spa>2424</spa>
</lang>
</name2>
<name3>
<type id ="3">
<coord>23432</coord>
<pmc>14324</pmc>
</type>
<lang>
<eng>141</eng>
<spa>142</spa>
</lang>
</name3>
假设您想要获取具有 pmc
子节点的任何 type
节点的属性 id
值,您可以使用以下 XPath 表达式:
//*/type[pmc]/@id
英文:
Since the question isn't entirely clear, let's try it this way.
Assuming that your xml reads like this:
<?xml version="1.0" encoding="UTF-8"?>
<name1>
<type id ="1">
<coord>67</coord>
<lmc>57657</lmc>
</type>
<lang>
<eng>989</eng>
<spa>123</spa>
</lang>
</name1>
<name2>
<type id ="2">
<coord>534</coord>
<lmc>654654</lmc>
</type>
<lang>
<eng>354</eng>
<spa>2424</spa>
</lang>
</name2>
<name3>
<type id ="3">
<coord>23432</coord>
<pmc>14324</pmc>
</type>
<lang>
<eng>141</eng>
<spa>142</spa>
</lang>
</name3>
and that you are trying to get the value of the attribute <id>
of any <type>
node which has a <pmc>
child node, try using the following as your xpath expression:
//*/type[pmc]/@id
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论