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?
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.
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:
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?
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.
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
}
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With