Searching...
Wednesday, 19 September 2012

Java XML JDOM Reading XML Example

04:24

Java XML JDOM Reading XML Example

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

Sample xml file to read

<?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>
Create Java class to Read XML file
package mycollectiontest;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class XMLParser {

public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("orderfile.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List list = rootNode.getChildren("OrderNumber");
for (int i = 0; i < list.size(); i++) {
Element node = (Element) list.get(i);
System.out.println("==============================");
System.out.println("OrderItem : "+ node.getChildText("OrderItem"));
System.out.println("OrderItemDescription : "+ node.getChildText("OrderItemDescription"));
System.out.println("OrderItemPrice : "+ node.getChildText("OrderItemPrice"));
System.out.println("OrderItemTax : "+ node.getChildText("OrderItemTax"));
System.out.println("OrderItemDiscount : "+ node.getChildText("OrderItemDiscount"));
System.out.println("OrderItemTotal : "+ node.getChildText("OrderItemTotal"));
System.out.println("==============================");
}
} catch (IOException ioException) {
System.out.println(ioException.getMessage());
} catch (JDOMException jdomException) {
System.out.println(jdomException.getMessage());
}}

}

Output

==============================
OrderItem : Phone
OrderItemDescription : Smart phone
OrderItemPrice : 7000
OrderItemTax : 700
OrderItemDiscount : 700
OrderItemTotal : 5600
==============================
==============================
OrderItem : Laptop
OrderItemDescription : 13 Inch
OrderItemPrice : 17000
OrderItemTax : 1700
OrderItemDiscount : 1700
OrderItemTotal : 15600
==============================

Reference

http://www.jdom.org
Wikipedia

0 comments: