4

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.

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

This is for clarification. If you just need to output the student names and nothing else, you can of course simple change the xpath query, as R.yan suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.