Searching...
Wednesday, 19 September 2012

Java XML DOM Parser Creating XML Example

02:51
DOM Parser classes

DocumentBuilder –> This class defines the API to obtain DOM Document instances from an XML document.

DocumentBuilderFactory –> Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.

Transformer –> An instance of this abstract class can transform a source tree into a result tree.

TransformerFactory –> A TransformerFactory instance can be used to create Transformer and Template objects.

DOM Parser Example (Creating xml file)
package mycollectiontest;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLParser {

public static void main(String[] args) {
try {

// Creating document builder
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// Creating Root element
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Order");
doc.appendChild(rootElement);

// Creating Order number
Element orderNumber = doc.createElement("Ordernumber");
rootElement.appendChild(orderNumber);

// Creating attribute to Order number element
Attr attr = doc.createAttribute("Number");
attr.setValue("9988");
orderNumber.setAttributeNode(attr);

// Creating Order Line elements
Element orderdItem = doc.createElement("OrderedItem1");
orderdItem.appendChild(doc.createTextNode("Pepsi"));
orderNumber.appendChild(orderdItem);
// orderlineTax elements
Element orderlinePrice = doc.createElement("OrderitemPrice");
orderlinePrice.appendChild(doc.createTextNode("10"));
orderNumber.appendChild(orderlinePrice);
// orderlineTax elements
Element orderlineTax = doc.createElement("OrderitemTax");
orderlineTax.appendChild(doc.createTextNode("10"));
orderNumber.appendChild(orderlineTax);

// Order line Discount
Element orderDiscount = doc.createElement("OrderDiscount");
orderDiscount.appendChild(doc.createTextNode("20"));
orderNumber.appendChild(orderDiscount);

// Create xmlfile with above attributes
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult resultXML = new StreamResult(new File("Order.xml"));
transformer.transform(source, resultXML);
System.out.println("Order.xml has been created");

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}

}
Output

<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
<Order>
<Ordernumber Number="9988">
<OrderedItem1>Pepsi</OrderedItem1>
<OrderitemPrice>10</OrderitemPrice>
<OrderitemTax>10</OrderitemTax>
<OrderDiscount>20</OrderDiscount>
</Ordernumber>
</Order>
Reference
docs.oracle.com

0 comments: