Searching...
Wednesday, 19 September 2012

Java XML JDOM Creating XML Example

04:29

What is JDOM ?

JDOM is an open source Java-based document object model for XML that was designed specifically for the Java platform so that it can take advantage of its language features. JDOM integrates with Document Object Model (DOM) and Simple API for XML (SAX), supports XPath and XSLT. It uses external parsers to build documents.

JDOM Readng XML Example

Create a Maven project and update below dependency

 <dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>

Or Download JDOM jar from http://www.jdom.org/downloads/index.html

Create Java file to create XML file

package mycollectiontest;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class XMLExample {

public static void main(String[] args) {
try {
//Creating Order Root element
Element order = new Element("Order");
Document doc = new Document(order);
doc.setRootElement(order);
// Creating OrderItem element
Element orderItem = new Element("OrderNumber");
// Setting Attributes
orderItem.setAttribute(new Attribute("number", "1000"));
// Setting contents
orderItem.addContent(new Element("OrderItem").setText("Phone"));
orderItem.addContent(new Element("OrderItemDescription")
.setText("Smart phone"));
orderItem.addContent(new Element("OrderItemPrice").setText("7000"));
orderItem.addContent(new Element("OrderItemTax").setText("700"));
orderItem.addContent(new Element("OrderItemDiscount")
.setText("700"));
orderItem.addContent(new Element("OrderItemTotal").setText("5600"));
// Adding orderItem element in to document
doc.getRootElement().addContent(orderItem);
// Creating next orderItem
Element orderItem1 = new Element("OrderNumber");
orderItem1.setAttribute(new Attribute("number", "1001"));
orderItem1.addContent(new Element("OrderItem").setText("Laptop"));
orderItem1.addContent(new Element("OrderItemDescription")
.setText("13 Inch"));
orderItem1.addContent(new Element("OrderItemPrice")
.setText("17000"));
orderItem1.addContent(new Element("OrderItemTax").setText("1700"));
orderItem1.addContent(new Element("OrderItemDiscount")
.setText("1700"));
orderItem1.addContent(new Element("OrderItemTotal")
.setText("15600"));

doc.getRootElement().addContent(orderItem1);
//Outputs a JDOM document as a stream of bytes. The outputter can manage many styles
//of document formatting, from untouched to pretty printed.
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("PretechJDOM.xml"));

System.out.println("PretechJDOM.xml.. has been created");
} catch (IOException io) {
System.out.println(io.getMessage());
}
}

}
Output

<?xmlversion="1.0"encoding="UTF-8"?>
<Order>
<OrderNumber number="1000">
<OrderItem>Phone</OrderItem>
<OrderItemDescription>Smart phone</OrderItemDescription>
<OrderItemPrice>7000</OrderItemPrice>
<OrderItemTax>700</OrderItemTax>
<OrderItemDiscount>700</OrderItemDiscount>
<OrderItemTotal>5600</OrderItemTotal>
</OrderNumber>
<OrderNumber number="1001">
<OrderItem>Laptop</OrderItem>
<OrderItemDescription>13 Inch</OrderItemDescription>
<OrderItemPrice>17000</OrderItemPrice>
<OrderItemTax>1700</OrderItemTax>
<OrderItemDiscount>1700</OrderItemDiscount>
<OrderItemTotal>15600</OrderItemTotal>
</OrderNumber>
</Order>

0 comments: