문제

I am pretty new in XPATH and I have the following problem:

I have the following snippet of code that seems not work as I expect:

                String XML = cdataContent;

                // Crea un nuovo documento XML a partire dal contenuto della
                // precedente sezione CDATA:
                documentXML = builder.build(new StringReader(XML));

                System.out.println(documentXML.toString());
                System.out.println("CDATA Content in XML document:");
                org.jdom.output.XMLOutputter xmlOutputterCDATAContent = new org.jdom.output.XMLOutputter(
                        org.jdom.output.Format.getPrettyFormat());
                xmlOutputter.output(documentXML, System.out);

                // Crea una nuova query XPATH per selezionare il valore del tag id e lo mette dentro l'oggetto AuthResponse:
                xPath = XPath.newInstance("/root/status/id");
                objectElement = (Element) xPath.selectSingleNode(documentXML);
                System.out.println("Y " + objectElement.getValue() + " Y");
                authResponse.setStatusResponse(objectElement.getValue());

                // Crea una nuova query XPATH per selezionare il valore del tag message: e lo mette dentro l'oggetto AuthResponse:
                XPath xPathMessage = XPath.newInstance("/root/status/message");
                Element objectElementMessage = (Element) xPath.selectSingleNode(documentXML);
                System.out.println("MESSAGE: " + objectElementMessage.getValue() + " /MESSAGE");
                authResponse.setStatusResponse(objectElementMessage.getValue());
            }
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

And this is the content of the documentXML variable:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <status>
    <id>-1</id>
    <message>Login o password errate</message>
  </status>
</root>

I print it by this lines of the previous code:

org.jdom.output.XMLOutputter xmlOutputterCDATAContent = new org.jdom.output.XMLOutputter( org.jdom.output.Format.getPrettyFormat());
xmlOutputter.output(documentXML, System.out);

Now I have to create 2 XPATH query: the first have to take the value of the id tag (in this case the -1 value) and the second that have to take the value inside the message tag (in this case the string value "Login o password errate")

Ok, as you can see in the posted snipped I have do:

1) XPATH QUERY TO OBTAIN THE id tag VALUE:

xPath = XPath.newInstance("/root/status/id");
objectElement = (Element) xPath.selectSingleNode(documentXML);
System.out.println("Y " + objectElement.getValue() + " Y");

This query seems to work well because in the Eclipse console is printed:

Y -1 Y

(So I know that the -1 value is taken properly)

2) XPATH QUERY TO OBATIN THE message tag VALUE:

Then I do:

XPath xPathMessage = XPath.newInstance("/root/status/message");
Element objectElementMessage = (Element) xPath.selectSingleNode(documentXML);
System.out.println("MESSAGE: " + objectElementMessage.getValue() + " /MESSAGE");

And now happen something strange because, in the Eclipse console, print the following output:

MESSAGE: -1 /MESSAGE

and not MESSAGE: Login o password errate /MESSAGE as I expect (because this is the value inside the message tag)

Why? What am I missing? What can I do to solve this situation?

Tnx

Andrea

도움이 되었습니까?

해결책

 XPath xPathMessage = XPath.newInstance("/root/status/message");
 Element objectElementMessage = (Element) xPath.selectSingleNode(documentXML);

change it to

XPath xPathMessage = XPath.newInstance("/root/status/message");
Element objectElementMessage = (Element) xPathMessage.selectSingleNode(documentXML);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top