Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to construct DOM and convert to XML?

$
0
0

DOM to XML  Constructing DOM (Document Object Model) to generate or represent XML is easy. The basic steps are as follows

 > Create the XML Document  object using the Document Builder Factory.

>Build your model by adding nodes and attributes etc. in the hierarchical order

> Once done, transform your XML Document to XML String representation by using an Transformer .

<!--break-->The following code snippet shows how to create DOM and then transform it into an XML String representation using the transformer. At run time the JVM would pick up the configured DocumentBuilder and as well as the Transformer.

If any one of these implementation is not found, it would throw a RuntimeException like NoClassDefFoundException and abort.

Home Insurance

package com.livrona.snippets.util;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
* This snippet shows how to create a DOM XML tree and then print the XML as
* String.
*
*/

publicclass DomXmlExample {

/**
* Test method
*
* @param args
*/

publicstaticvoid main(String args[]) {
new DomXmlExample();
}

public DomXmlExample() {
try {

/**
* XML to Create
*/

// <root>
// <!--This is root node-->
// <child name="value">This is child node value</child>
// </root>
// Create an empty XML Document
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();

// Create the XML tree

// create the root element and add it to the document
Element root = doc.createElement("root");
doc.appendChild(root);

// create a comment and put it in the root element
Comment comment = doc.createComment("This is root node");
root.appendChild(comment);

// create child element, add an attribute, and add to root
Element child = doc.createElement("child");
child.setAttribute("name", "value");
root.appendChild(child);

// add a text element to the child
Text text = doc.createTextNode("This is child node value");
child.appendChild(text);

// Output the XML

// set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

// create string from xml tree
StringWriter sw =new StringWriter();
// set the String Writer to StreamResult
StreamResult result =new StreamResult(sw);

// create dom source from dom document
DOMSource source =new DOMSource(doc);
// transform dom source into Stream Result
trans.transform(source, result);
String xmlString = sw.toString();

// print xml
System.out.println("XML:\n"+ xmlString);

} catch (Exception e) {
System.out.println("Error generating xml : "+ e);
e.printStackTrace();

}
}
}
The following is the output xml of the above snippet.
<root>
    <!--This is root node-->
    <child name="value">This is child node value</child>
</root>



Viewing all articles
Browse latest Browse all 178

Trending Articles