Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java convert string to xml and parse node [duplicate]

Hello I am getting back a string from a webservice.

I need to parse this string and get the text in error message?

My string looks like this:

<response>
<returnCode>-2</returnCode>
<error>
<errorCode>100</errorCode>
<errorMessage>ERROR HERE!!!</errorMessage>
</error>
</response>

Is it better to just parse the string or convert to xml then parse?

like image 378
Doc Holiday Avatar asked Oct 29 '13 14:10

Doc Holiday


People also ask

How to convert string to XML document in Java?

1) Convert String to XML Document. To convert XML string to XML Dom, we need following classes: javax.xml.parsers.DocumentBuilder : Defines the API to obtain XML DOM Document instances from an XML content from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.

How to parse and process XML documents in Java?

To parse and process XML documents, Java provides several libraries. The essential functionality to read and change an XML file is provided by Java xml parser examples. How does Java XML Parser Work? When it comes to parsing XML documents, Java has a lot of possibilities. The following are some of the most widely used Java XML parsers:

How to convert string newNode to XML?

String newNode = "<node>value</node>"; // Convert this to XML Then insert this node into an org.w3c.dom.Documentas the child of a given node?

How to read XML with DOM parser?

DOM Parser API -Import XML-related packages -Create a DocumentBuilder -Create a Document from a file or stream -Validate Document structure -Extract the root element -Examine attributes -Examine sub-elements 2. Read XML with DOM parser 3. Read data to POJO objects 4.


3 Answers

I'd use Java's XML document libraries. It's a bit of a mess, but works.

String xml = "<response>\n" +
             "<returnCode>-2</returnCode>\n" +
             "<error>\n" +
             "<errorCode>100</errorCode>\n" +
             "<errorMessage>ERROR HERE!!!</errorMessage>\n" +
             "</error>\n" +
             "</response>";
Document doc = DocumentBuilderFactory.newInstance()
                                     .newDocumentBuilder()
                                     .parse(new InputSource(new StringReader(xml)));

NodeList errNodes = doc.getElementsByTagName("error");
if (errNodes.getLength() > 0) {
    Element err = (Element)errNodes.item(0);
    System.out.println(err.getElementsByTagName("errorMessage")
                          .item(0)
                          .getTextContent());
} else { 
        // success
}
like image 117
azz Avatar answered Oct 27 '22 21:10

azz


I would probably use an XML parser to convert it into XML using DOM, then get the text. This has the advantage of being robust and coping with any unusual situations such as a line like this, where something has been commented out:

<!-- commented out <errorMessage>ERROR HERE!!!</errorMessage> -->

If you try and parse it yourself then you might fall foul of things like this. Also it has the advantage that if the requirements expand, then its really easy to change your code.

http://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm

like image 2
Paul Richards Avatar answered Oct 27 '22 21:10

Paul Richards


It's an XML document. Use an XML parser.

You could tease it apart using string operations. But you have to worry about entity decoding, character encodings, CDATA sections etc. An XML parser will do all of this for you.

Check out JDOM for a simpler XML parsing approach than using raw DOM/SAX implementations.

like image 1
Brian Agnew Avatar answered Oct 27 '22 23:10

Brian Agnew