In Java, XML is represented with org.w3c.dom.Document
object. In this XML tutorial, we will learn to –
- Convert XML string to XML Document
- Convert XML file content to XML Document
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.
- javax.xml.parsers.DocumentBuilderFactory : Defines a factory API that enables applications to obtain a parser (
DocumentBuilder
) that produces DOM object trees from XML content. - org.w3c.dom.Document : It represents the entire XML DOM. Conceptually, it is the root of the document tree, and provides the access to the document’s data further down into the tree, through factory methods.
- java.io.StringReader : Create a stream from String content.
DocumentBuilder
uses this stream to read XML content for parsing.
package com.howtodoinjava.demo; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class StringtoXMLExample { public static void main(String[] args) { final String xmlStr = "<employees>" + " <employee id=\"101\">" + " <name>Lokesh Gupta</name>" + " <title>Author</title>" + " </employee>" + " <employee id=\"102\">" + " <name>Brian Lara</name>" + " <title>Cricketer</title>" + " </employee>" + "</employees>"; //Use method to convert XML string content to XML Document object Document doc = convertStringToXMLDocument( xmlStr ); //Verify XML document is build correctly System.out.println(doc.getFirstChild().getNodeName()); } private static Document convertStringToXMLDocument(String xmlString) { //Parser that produces DOM object trees from XML content DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //API to obtain DOM Document instance DocumentBuilder builder = null; try { //Create DocumentBuilder with default configuration builder = factory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new InputSource(new StringReader(xmlString))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } } //Output: employees
2) Convert XML file to XML Document
To get the XML dom from XML file, instead of passing XML string to DocumentBuilder
, pass the file path to let parser read the file content directly.
We have employees.xml
file which has XML content, we will read to get XML document.
<employees> <employee id="101"> <name>Lokesh Gupta</name> <title>Author</title> </employee> <employee id="102"> <name>Brian Lara</name> <title>Cricketer</title> </employee> </employees>
package com.howtodoinjava.demo; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class StringtoXMLExample { public static void main(String[] args) { final String xmlFilePath = "employees.xml"; //Use method to convert XML string content to XML Document object Document doc = convertXMLFileToXMLDocument( xmlFilePath ); //Verify XML document is build correctly System.out.println(doc.getFirstChild().getNodeName()); } private static Document convertXMLFileToXMLDocument(String filePath) { //Parser that produces DOM object trees from XML content DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //API to obtain DOM Document instance DocumentBuilder builder = null; try { //Create DocumentBuilder with default configuration builder = factory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new File(filePath)); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } } //Output: employees
Drop me your questions in comments section.
Happy Learning !!
Leave a Reply