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

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

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(&quot;test.xml&quot;)); 

List&lt;String&gt; data = new ArrayList&lt;&gt;();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate(&quot;/name1/type/*[name()!= &#39;pmc&#39;]&quot;, document, XPathConstants.NODESET);
        for (int i = 0; i &lt; nodes.getLength(); i++) {
            Element el = (Element) nodes.item(i);
            data.add(el.getAttribute(&quot;id&quot;));
        }

System.out.println(data);

test.xml :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;name1&gt;
    &lt;type id =&quot;1&quot;&gt;
        &lt;coord&gt;67&lt;/coord&gt;
        &lt;lmc&gt;57657&lt;/lmc&gt;
    &lt;/type&gt;
    &lt;lang&gt;
        &lt;eng&gt;989&lt;/eng&gt;
        &lt;spa&gt;123&lt;/spa&gt;
    &lt;/lang&gt;
&lt;/name1&gt;
&lt;name2&gt;
    &lt;type id =&quot;2&quot;&gt;
        &lt;coord&gt;534&lt;/coord&gt;
        &lt;lmc&gt;654654&lt;/lmc&gt;
    &lt;/type&gt;
    &lt;lang&gt;
        &lt;eng&gt;354&lt;/eng&gt;
        &lt;spa&gt;2424&lt;/spa&gt;
    &lt;/lang&gt;
&lt;/name2&gt;
&lt;name3&gt;
    &lt;type&gt;
        &lt;coord&gt;23432&lt;/coord&gt;
        &lt;pmc&gt;14324&lt;/pmc&gt;
    &lt;/type&gt;
    &lt;lang&gt;
        &lt;eng&gt;141&lt;/eng&gt;
        &lt;spa&gt;142&lt;/spa&gt;
    &lt;/lang&gt;
&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 内容如下:

<?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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;name1&gt;
    &lt;type id =&quot;1&quot;&gt;
        &lt;coord&gt;67&lt;/coord&gt;
        &lt;lmc&gt;57657&lt;/lmc&gt;
    &lt;/type&gt;
    &lt;lang&gt;
        &lt;eng&gt;989&lt;/eng&gt;
        &lt;spa&gt;123&lt;/spa&gt;
    &lt;/lang&gt;
&lt;/name1&gt;
&lt;name2&gt;
    &lt;type id =&quot;2&quot;&gt;
        &lt;coord&gt;534&lt;/coord&gt;
        &lt;lmc&gt;654654&lt;/lmc&gt;
    &lt;/type&gt;
    &lt;lang&gt;
        &lt;eng&gt;354&lt;/eng&gt;
        &lt;spa&gt;2424&lt;/spa&gt;
    &lt;/lang&gt;
&lt;/name2&gt;
&lt;name3&gt;
    &lt;type id =&quot;3&quot;&gt;
        &lt;coord&gt;23432&lt;/coord&gt;
        &lt;pmc&gt;14324&lt;/pmc&gt;
    &lt;/type&gt;
    &lt;lang&gt;
        &lt;eng&gt;141&lt;/eng&gt;
        &lt;spa&gt;142&lt;/spa&gt;
    &lt;/lang&gt;
&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:

//*/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:

确定