简体   繁体   English

Java中的Xpath筛选器仅重新带回第一个值结果

[英]Xpath Filter in Java Brings Back Only the First Value Result Reapitly

I'm new here and to make it short i have this problem. 我是新来的,简短地说,我有这个问题。 I want to get all values of the Child's that have an attribute"@name='Priority' as an list and i dit this: 我想获取具有属性“ @ name ='Priority'作为列表的Child's的所有值,然后我将其删除:

    XPathExpression expr = xpath.compile ("//*[@name='Priority']");
        NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
         System.out.println("Found " + nl.getLength() );
            for(int i = 0; i < nl.getLength(); i++) {
                System.out.println("Priority:" + xpath.compile("//*[@name='Priority']").evaluate(nl.item(i)));
                System.out.println("==================");
            }

my XML structure here 我的XML结构在这里

  <INSTANCE id="obj.43200" class="Car" name="Car-43200">
        <ATTRIBUTE name="Position" type="STRING">NODE x:15.5cm y:7cm w:1.5cm h:1cm index:69</ATTRIBUTE>
        <ATTRIBUTE name="External tool coupling" type="STRING" />
        <ATTRIBUTE name="Direction" type="ENUMERATION">Horizontal</ATTRIBUTE>
        <ATTRIBUTE name="Priority" type="INTEGER">40</ATTRIBUTE>
     </INSTANCE>

but when i execute the Java Code in eclips i get this: 但是当我在Eclipse中执行Java代码时我得到了这个:

    Found 9
Priority:40
==================
Priority:40
==================
Priority:40
==================
Priority:40
==================
Priority:40
==================
Priority:40
==================
Priority:40
==================
Priority:40
==================
Priority:40
==================

it brings me only the first result as the end result for all other 8 XPath filter results. 对于所有其他8个XPath过滤器结果,它仅带给我第一个结果作为最终结果。 It should be like this for exmp: 对于exmp应该是这样的:

     Found 2
Priority:40
==================
Priority:5

and so on... 等等...

What could I do? 我能做什么? Thanks in advance :) 提前致谢 :)

based on the given xml content and java code, output as following 基于给定的xml内容和java代码,输出如下

Found 1
Priority:40
==================

i have a no idea about your xml content. 我对您的xml内容一无所知。 please try with this 请尝试这个

XPathExpression expr = xpath.compile ("//*[@name='Priority']");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
System.out.println("Found " + nl.getLength() );
for(int i = 0; i < nl.getLength(); i++) {
    System.out.println("Priority:" + nl.item(i).getTextContent());
    System.out.println("==================");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM