1

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("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>

For example I wouldlike to get only the id if the childnode is 'pmc' and not the others.

7
  • Your xml is invalid (for example <pmc>14324</umc>). Can you edit your question and fix it? Commented Sep 20, 2020 at 20:33
  • What exactly is the problem? Does your code run? Are there errors? Then post the error message and the full stack trace. Commented Sep 20, 2020 at 21:33
  • @vanje No error, I just can't get the data as I wouldlike Commented Sep 20, 2020 at 21:42
  • Then you should mention this in your question. And please include the expected result and the actual result. In the predicate do you mean the function name()? Then you should include the parentheses. Commented Sep 20, 2020 at 21:51
  • @vanje yes sorry... yes I mean name() but it seems != not working.. Commented Sep 20, 2020 at 22:00

1 Answer 1

1

Since the question isn't entirely clear, let's try it this way.

Assuming that your xml reads like this:

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

and that you are trying to get the value of the attribute <id> of any <type> node which has a <pmc> child node, try using the following as your xpath expression:

//*/type[pmc]/@id
Sign up to request clarification or add additional context in comments.

Comments

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.