XPath 读取指定属性标签的值

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

Xpath read given attribute tag value

问题

Sure, here is the translated code part:

String exp = "//MS_xml_root/message/field";
NodeList list = (NodeList) xPath.compile(exp).evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
    // 需要在这里读取值。
}

Please let me know if you need any further assistance with this code.

英文:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;MS_xml_root&gt;
&lt;message&gt;
&lt;field id=&quot;A&quot;&gt;A&lt;/field&gt;
&lt;field id=&quot;B&quot;&gt;B&lt;/field&gt;
&lt;field id=&quot;C&quot;&gt;&lt;/field&gt;
&lt;/message&gt;
&lt;message&gt;
&lt;field id=&quot;A&quot;&gt;A&lt;/field&gt;
&lt;field id=&quot;B&quot;&gt;B&lt;/field&gt;
&lt;field id=&quot;C&quot;&gt;&lt;/field&gt;
&lt;/message&gt;
&lt;/MS_xml_root&gt;

I want to read the field tag value by giving the field id key using XPath. But I can read the attribute value.

String exp = &quot;//MS_xml_root/message/field&quot;
NodeList list = (NodeList) xPath.compile(exp).evaulate(doc,XPathConstants.NODESET);
for(int i=0;i &lt; list.getLength(); i++){
//need to read the value here.
}

答案1

得分: 1

String id = "A";
String exp = "//MS_xml_root/message/field[@id='" + id + "']";

将获取具有 id="A" 的节点。要获取属性值,可以使用以下方式:

String exp = "//MS_xml_root/message/field/@id";
英文:
String id = &quot;A&quot;;
String exp = &quot;//MS_xml_root/message/field[@id=&#39;&quot; + id + &quot;&#39;]&quot;;

will get the nodes with id=&quot;A&quot;. Use NODE instead of NODESET to get a single Node.

If you want the attribute value, use something like:

String exp = &quot;//MS_xml_root/message/field/@id&quot;;

答案2

得分: 1

看起来您需要获取具有特定ID的节点的TextContent的Java方法。
您可以使用以下代码来实现:

private static List<String> getFieldNodeValues(Document doc, String id) throws XPathExpressionException {
    String exp = "//MS_xml_root/message/field[@id='" + id + "']";
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList list = (NodeList) xpath.compile(exp).evaluate(doc, XPathConstants.NODESET);
    List<String> res = new ArrayList<>();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        res.add(node.getTextContent());
    }
    return res;
}

您可能需要的是值的列表,而不仅仅是一个值,因为您的XML似乎对于给定的ID值有多个字段。

英文:

It seems you need the Java method to get the TextContent of nodes which have a specific ID.
You could do it with below code:

private static List&lt;String&gt; getFieldNodeValues(Document doc, String id) throws XPathExpressionException {
    	
    	String exp = &quot;//MS_xml_root/message/field[@id=&quot; + &quot;\&quot;&quot; + id + &quot;\&quot;&quot; + &quot;]&quot;;
    	XPath xpath = XPathFactory.newInstance().newXPath();
    	NodeList list = (NodeList) xpath.compile(exp).evaluate(doc,XPathConstants.NODESET);
    	List&lt;String&gt; res = new ArrayList&lt;&gt;();
        for ( int i = 0; i &lt; list.getLength(); i++ ) {
        	Node node = list.item(i);
        	res.add(node.getTextContent());
        }
        return res;
    }

You may want a list of values not one value, because your xml seems to have more than one field for a given ID value.

huangapple
  • 本文由 发表于 2020年7月31日 23:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63194586.html
匿名

发表评论

匿名网友

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

确定