Ever caught into a situation where you are required to create an object from your response XML? If yes, This post can be useful for you.

This tutorial shows you how to Convert String XML fragment to Document Node in Java. Consider the below example code:

/****************************************************************

* How to use turn an XML file into a document object in Java

****************************************************************/

DocumentBuilderFactory docBuilderFactory =

DocumentBuilderFactory.newInstance();

docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

// Parse the XML file and build the Document object in RAM

Document doc = docBuilder.parse(new StringBufferInputStream("<Name>JavaGenious</Name>));

// Normalize text representation.

// Collapses adjacent text nodes into one node.

doc.getDocumentElement().normalize();

 

The XML Nodelist can be obtained from this Document object which can be iterated using a Java for loop to read the enitre XML data. Moreover, You can use XPath to query the response XML using a XPath Expression. Know more about XPath in our, XPath Tutorial Section


Enter your email address:

Delivered by FeedBurner



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)