简体   繁体   中英

Java, XPath - how to get the entire node base on specific element

I have the following XML structure

<students>
<student studentnumber="1">
    <firstname>Charlie</firstname>
    <lastname>Davies</lastname>
    <marks>
        <first>12</first>
        <second>52</second>
        <third>98</third>
        <forth>32</forth>
    </marks>
</student>
<student studentnumber="2">
    <firstname>Emily</firstname>
    <lastname>Roberts</lastname>
    <marks>
        <first>55</first>
        <second>51</second>
        <third>57</third>
        <forth>84</forth>
    </marks>
</student>

How can I construct the query to get the first name of students who have the first mark over 50? I'm doing this and I can see there is one result which what I expect but it doesn't print out anything.

    String expression = "/students/student[marks/first > 50]";
    NodeList nodes = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);

    System.out.println(nodes.getLength());

    for (int i = 0; i < nodes.getLength(); ++i) {
        System.out.println(nodes.item(i).getFirstChild().getNodeValue());
    }

Thanks.

you just need to simply change your Xpath expression to follow: String expression = "/students/student[marks/first > 50]/firstname/text()"

And your for-loop to:

for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getNodeValue());
}

Output:

1
Emily

GetFirstChild() and getChildNodes() yield not only child element nodes but also text nodes. The first node you get will be a text node (just whitespace characters and a line break).

Example:

String expression = "/students/student[marks/first > 50]";
NodeList nodes = (NodeList) 
xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);


 for (int i = 0; i < nodes.getLength(); ++i) {
        Node n = nodes.item(i);
        for (int j = 0; j < n.getChildNodes().getLength(); ++j) {
            Node n1 = n.getChildNodes().item(j);
            System.out.println("Level 1 node type: " + n1.getNodeType());
            System.out.println("Level 1 node value: " + n1.getNodeValue());
            for (int k = 0; k < n1.getChildNodes().getLength(); ++k) {
                Node n2 = n1.getChildNodes().item(k);
                System.out.println("  Level 2 node type: " + n2.getNodeType());
                System.out.println("  Level 2 Node value: " + n2.getNodeValue());
            }
        }
    }

Output:

1
Level 1 node type: 3
Level 1 node value: '
    '
Level 1 node type: 1
Level 1 node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: 'Emily'
Level 1 node type: 3
Level 1 node value: '
    '
Level 1 node type: 1
Level 1 node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: 'Roberts'
Level 1 node type: 3
Level 1 node value: '
    '
Level 1 node type: 1
Level 1 node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: '
        '
  Level 2 node type: 1
  Level 2 Node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: '
        '
  Level 2 node type: 1
  Level 2 Node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: '
        '
  Level 2 node type: 1
  Level 2 Node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: '
        '
  Level 2 node type: 1
  Level 2 Node value: 'null'
  Level 2 node type: 3
  Level 2 Node value: '
    '
Level 1 node type: 3
Level 1 node value: '
'

EDIT: Changed example to output student names, not student marks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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