Feature Image-Extract Values From contents of XML

How to Extract Values From Contents Of XML Document

In this post, we will learn how can we extract values from contents of XML output string using JAVA. XML outputs are mostly found from the web services. So, to perform logical operations using the values of the output, first, we need to extract values from contents of XML string. We will see how we can do the same.

Extract Values from Contents Of XML - JAVA Code

import java.io.StringReader;
import org.xml.sax.InputSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

public class Test {

public static void main(String[] args) {

try {
String xml="<securityQuestionAnswer><status>SUCCESS</status><message /><systemErrorMessage /><httpStatusCode>202</httpStatusCode><userID>[email protected]</userID><attemptCounter>0</attemptCounter></securityQuestionAnswer>";
InputSource source=new InputSource(new StringReader(xml));
XPath xpath=XPathFactory.newInstance().newXPath();
Object securityQuestionAnswer=xpath.evaluate("/securityQuestionAnswer",source,XPathConstants.NODE);
String status=xpath.evaluate("status", securityQuestionAnswer);
String code=xpath.evaluate("httpStatusCode", securityQuestionAnswer);
String attemptCounter=xpath.evaluate("attemptCounter", securityQuestionAnswer);
String userId=xpath.evaluate("userID", securityQuestionAnswer);
System.out.println("status="+status+" code="+code+" UserId="+userId+" AttemptCounter="+attemptCounter);

} catch (Exception e) {
// TODO Auto-generated catch block


From the above code, we can see that we need to create a Java class, followed by writing the code inside the main method. At first, the String xml contains the output XML which is fed to InputSource, which is a single input source for an XML entity. 

The SAX parser will use the InputSource object to determine how to read XML input. If there is a character stream available, the parser will read that stream directly, disregarding any text encoding declaration found in that stream. In case there is no character stream, but there is a byte stream, the parser will use that byte stream, using the encoding specified in the InputSource. Else (if no encoding is specified) autodetect the character encoding using an algorithm such as the one in the XML specification. And if neither a character stream nor a byte stream is available, the parser will attempt to open a URI connection to the resource identified by the system identifier.

Next, a new instance of Xpath is created, which is a query language to select nodes from XML documents. Moreover, Xpaths are also required to compute values from the contents of the XML document.

Post this we extract the value of the root element securityQuestionAnswer and then using it we extract all the children elements present in the XML. Below we have the output of our code.

Extract Values from Contents Of XML - Output of Code


status=SUCCESS code=202 [email protected] AttemptCounter=0

Hope you liked this article and learned something. If you did, do not forget to share and comment below. Also, check our other posts.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.