获取只有在子节点不同时才获取值的Java XPath表达式

huangapple go评论103阅读模式
英文:

java xpath expression get only value if child node is different

问题

我想要获取一个特定节点的值,如果子节点不同于我表达式中的内容:

  1. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  2. DocumentBuilder builder = factory.newDocumentBuilder();
  3. document = builder.parse(new File("test.xml"));
  4. List<String> data = new ArrayList<>();
  5. XPath xpath = XPathFactory.newInstance().newXPath();
  6. NodeList nodes = (NodeList) xpath.evaluate("/name1/type/*[name()!= 'pmc']", document, XPathConstants.NODESET);
  7. for (int i = 0; i < nodes.getLength(); i++) {
  8. Element el = (Element) nodes.item(i);
  9. data.add(el.getAttribute("id"));
  10. }
  11. System.out.println(data);

test.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <name1>
  3. <type id ="1">
  4. <coord>67</coord>
  5. <lmc>57657</lmc>
  6. </type>
  7. <lang>
  8. <eng>989</eng>
  9. <spa>123</spa>
  10. </lang>
  11. </name1>
  12. <name2>
  13. <type id ="2">
  14. <coord>534</coord>
  15. <lmc>654654</lmc>
  16. </type>
  17. <lang>
  18. <eng>354</eng>
  19. <spa>2424</spa>
  20. </lang>
  21. </name2>
  22. <name3>
  23. <type>
  24. <coord>23432</coord>
  25. <pmc>14324</pmc>
  26. </type>
  27. <lang>
  28. <eng>141</eng>
  29. <spa>142</spa>
  30. </lang>
  31. </name3>

例如,我只想获取具有子节点 pmc 而不是其他节点的 id 值。

英文:

I woudlike to get one specific value of node if child node is different like present in my expression :

  1. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  2. DocumentBuilder builder = factory.newDocumentBuilder();
  3. document = builder.parse(new File(&quot;test.xml&quot;));
  4. List&lt;String&gt; data = new ArrayList&lt;&gt;();
  5. XPath xpath = XPathFactory.newInstance().newXPath();
  6. NodeList nodes = (NodeList) xpath.evaluate(&quot;/name1/type/*[name()!= &#39;pmc&#39;]&quot;, document, XPathConstants.NODESET);
  7. for (int i = 0; i &lt; nodes.getLength(); i++) {
  8. Element el = (Element) nodes.item(i);
  9. data.add(el.getAttribute(&quot;id&quot;));
  10. }
  11. System.out.println(data);

test.xml :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;name1&gt;
  3. &lt;type id =&quot;1&quot;&gt;
  4. &lt;coord&gt;67&lt;/coord&gt;
  5. &lt;lmc&gt;57657&lt;/lmc&gt;
  6. &lt;/type&gt;
  7. &lt;lang&gt;
  8. &lt;eng&gt;989&lt;/eng&gt;
  9. &lt;spa&gt;123&lt;/spa&gt;
  10. &lt;/lang&gt;
  11. &lt;/name1&gt;
  12. &lt;name2&gt;
  13. &lt;type id =&quot;2&quot;&gt;
  14. &lt;coord&gt;534&lt;/coord&gt;
  15. &lt;lmc&gt;654654&lt;/lmc&gt;
  16. &lt;/type&gt;
  17. &lt;lang&gt;
  18. &lt;eng&gt;354&lt;/eng&gt;
  19. &lt;spa&gt;2424&lt;/spa&gt;
  20. &lt;/lang&gt;
  21. &lt;/name2&gt;
  22. &lt;name3&gt;
  23. &lt;type&gt;
  24. &lt;coord&gt;23432&lt;/coord&gt;
  25. &lt;pmc&gt;14324&lt;/pmc&gt;
  26. &lt;/type&gt;
  27. &lt;lang&gt;
  28. &lt;eng&gt;141&lt;/eng&gt;
  29. &lt;spa&gt;142&lt;/spa&gt;
  30. &lt;/lang&gt;
  31. &lt;/name3&gt;

For example I wouldlike to get only the id if the childnode is &#39;pmc&#39; and not the others.

答案1

得分: 1

因为问题并不是十分清楚,我们试着这样来处理。

假设您的 XML 内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <name1>
  3. <type id ="1">
  4. <coord>67</coord>
  5. <lmc>57657</lmc>
  6. </type>
  7. <lang>
  8. <eng>989</eng>
  9. <spa>123</spa>
  10. </lang>
  11. </name1>
  12. <name2>
  13. <type id ="2">
  14. <coord>534</coord>
  15. <lmc>654654</lmc>
  16. </type>
  17. <lang>
  18. <eng>354</eng>
  19. <spa>2424</spa>
  20. </lang>
  21. </name2>
  22. <name3>
  23. <type id ="3">
  24. <coord>23432</coord>
  25. <pmc>14324</pmc>
  26. </type>
  27. <lang>
  28. <eng>141</eng>
  29. <spa>142</spa>
  30. </lang>
  31. </name3>

假设您想要获取具有 pmc 子节点的任何 type 节点的属性 id 值,您可以使用以下 XPath 表达式:

  1. //*/type[pmc]/@id
英文:

Since the question isn't entirely clear, let's try it this way.

Assuming that your xml reads like this:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;name1&gt;
  3. &lt;type id =&quot;1&quot;&gt;
  4. &lt;coord&gt;67&lt;/coord&gt;
  5. &lt;lmc&gt;57657&lt;/lmc&gt;
  6. &lt;/type&gt;
  7. &lt;lang&gt;
  8. &lt;eng&gt;989&lt;/eng&gt;
  9. &lt;spa&gt;123&lt;/spa&gt;
  10. &lt;/lang&gt;
  11. &lt;/name1&gt;
  12. &lt;name2&gt;
  13. &lt;type id =&quot;2&quot;&gt;
  14. &lt;coord&gt;534&lt;/coord&gt;
  15. &lt;lmc&gt;654654&lt;/lmc&gt;
  16. &lt;/type&gt;
  17. &lt;lang&gt;
  18. &lt;eng&gt;354&lt;/eng&gt;
  19. &lt;spa&gt;2424&lt;/spa&gt;
  20. &lt;/lang&gt;
  21. &lt;/name2&gt;
  22. &lt;name3&gt;
  23. &lt;type id =&quot;3&quot;&gt;
  24. &lt;coord&gt;23432&lt;/coord&gt;
  25. &lt;pmc&gt;14324&lt;/pmc&gt;
  26. &lt;/type&gt;
  27. &lt;lang&gt;
  28. &lt;eng&gt;141&lt;/eng&gt;
  29. &lt;spa&gt;142&lt;/spa&gt;
  30. &lt;/lang&gt;
  31. &lt;/name3&gt;

and that you are trying to get the value of the attribute &lt;id&gt; of any &lt;type&gt; node which has a &lt;pmc&gt; child node, try using the following as your xpath expression:

  1. //*/type[pmc]/@id

huangapple
  • 本文由 发表于 2020年9月21日 02:21:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63982272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定