JDOM
Questions:- What is JDOM?
Answer:- Java + XML = JDOM
JDOM is an open source JAVA API.
It provides a way to manipulate XML files within java program or it allow XML
parsing and reading within a java program. It is designed by using java
programming techniques and customized for java developers. JDOM is easy to
understand. JDOM - a Java XML parser is more user friendly in the way of
accessing the XML document. The JDOM API supports XPath expression to query XML
documents and it also supports XSLT. It has
a straightforward API. It is lightweight and fast. It is optimized for the Java
programmer.JDOM was developed by Jason Hunter and Brett McLaughlin. As it
integrates well with DOM and SAX, so we can say that it provides an alternative
to DOM and SAX.
Note: -
XPath (XML Path Language)
- It is a query language for selecting nodes from an XML document. It is a
language for addressing XML document’s elements and attributes. It may be used
for the computation of values (e.g., strings, numbers, or Boolean values) from
the content of an XML document. It is defined by W3C consortium.
XSLT stands for EXtensible
Stylesheet Language Transformations. It is providing a way to transform XML
documents into other formats such as HTML for web-page, plain text or into XSL
formatting objects. The original document is not changed rather a new document
is created based on the content of an existing document. With the help of XSLT
language we can perform computations. XSLT versions are 1.0, 2.0 and 3.0.
JDOM features:-
- It has a straightforward API
- It is a lightweight and fast as compare to DOM API and it also simplifies the interaction with the XML.
- It hides the complexities of XML.
- It is optimized for the Java developer.
- It supports reading and writing DOM documents and SAX events.
- It supports runtime plug-in of any DOM and SAX parser.
- It supports easy conversion from JDOM to DOM/SAX and vice-versa.
- If we are using JDOM for XML document manipulation then it doesn’t require in-depth XML knowledge.
JDOM API consists of four packages:-
In JDOM,
available classes to represent an XML document:
- Document
- Namespace
- Element
- Entity
- Attribute
- CDATA
- Comment
- DocType
- Content
- EntityRef
- Text
- Verifier
- ProcessingInstruction
To interact with DOM, DOM implementation classes in JDOM API are following
- XercesDOMAdapter
- XML4JDOMAdapter
- JAXPDOMAdapter
- AbstractDOMAdapter
- CrimsonDOMAdapter
- OracleV1DOMAdapter
- OracleV2DOMAdapter
Classes for
reading XML in JDOM API are as follows:
- DOMBuilder
- SAXBuilder
- StAXEventBuilder
- StAXStreamBuilder
Classes for
writing XML in JDOM API are as follows:
- DOMOutputter
- SAXOutputter
- XMLOutputter
- StAXEventOutputter
- StAXStreamOutputter
For more details please refer - http://www.jdom.org/downloads/ora01-jdom.pdf
Reference taken
from jdom.org and wikipedia.org
How to write an XML file using JDOM API?
Demo Example for writing an XML file using JDOM API:-
JDOMWriteXMLFile.java
package com.gaurav.jdom.examples;
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 JDOMWriteXMLFile {
public static void main(String[] args) {
try {
XMLOutputter xmlOutputter = new XMLOutputter();
Element employees = new Element("employees");
Document document = new Document(employees);
document.setRootElement(employees);
Element employee = new Element("employee");
employee.setAttribute(new Attribute("id", "1234"));
employee.addContent(new Element("name").setText("KUMAR GAURAV"));
employee.addContent(new Element("designation")
.setText("SYSTEM ENGINEER"));
employee.addContent(new Element("java-experience").setText("7"));
employee.addContent(new Element("salary").setText("50000"));
document.getRootElement().addContent(employee);
Element employee2 = new Element("employee");
employee2.setAttribute(new Attribute("id", "2341"));
employee2.addContent(new Element("name").setText("KUMAR AADITYA"));
employee2.addContent(new Element("designation")
.setText("PROGRAM MANAGER"));
employee2.addContent(new Element("java-experience").setText("15"));
employee2.addContent(new Element("salary").setText("7000000"));
document.getRootElement().addContent(employee2);
xmlOutputter.setFormat(Format.getPrettyFormat());
xmlOutputter.output(document, new FileWriter(
"C://GeneratedXMLUsingJDOM.xml"));
System.out.println("*** File Created successfully ***");
} catch (IOException io) {
System.out.println(io.getMessage());
}
}
}
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 JDOMWriteXMLFile {
public static void main(String[] args) {
try {
XMLOutputter xmlOutputter = new XMLOutputter();
Element employees = new Element("employees");
Document document = new Document(employees);
document.setRootElement(employees);
Element employee = new Element("employee");
employee.setAttribute(new Attribute("id", "1234"));
employee.addContent(new Element("name").setText("KUMAR GAURAV"));
employee.addContent(new Element("designation")
.setText("SYSTEM ENGINEER"));
employee.addContent(new Element("java-experience").setText("7"));
employee.addContent(new Element("salary").setText("50000"));
document.getRootElement().addContent(employee);
Element employee2 = new Element("employee");
employee2.setAttribute(new Attribute("id", "2341"));
employee2.addContent(new Element("name").setText("KUMAR AADITYA"));
employee2.addContent(new Element("designation")
.setText("PROGRAM MANAGER"));
employee2.addContent(new Element("java-experience").setText("15"));
employee2.addContent(new Element("salary").setText("7000000"));
document.getRootElement().addContent(employee2);
xmlOutputter.setFormat(Format.getPrettyFormat());
xmlOutputter.output(document, new FileWriter(
"C://GeneratedXMLUsingJDOM.xml"));
System.out.println("*** File Created successfully ***");
} catch (IOException io) {
System.out.println(io.getMessage());
}
}
}
Result:-
Content of the generated file GeneratedXMLUsingJDOM.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="1234">
<name>KUMAR GAURAV</name>
<designation>SYSTEM ENGINEER</designation>
<java-experience>7</java-experience>
<salary>50000</salary>
</employee>
<employee id="2341">
<name>KUMAR AADITYA</name>
<designation>PROGRAM MANAGER</designation>
<java-experience>15</java-experience>
<salary>7000000</salary>
</employee>
</employees>
<employees>
<employee id="1234">
<name>KUMAR GAURAV</name>
<designation>SYSTEM ENGINEER</designation>
<java-experience>7</java-experience>
<salary>50000</salary>
</employee>
<employee id="2341">
<name>KUMAR AADITYA</name>
<designation>PROGRAM MANAGER</designation>
<java-experience>15</java-experience>
<salary>7000000</salary>
</employee>
</employees>
How to read an XML file using JDOM API?
Demo Example for reading an XML file using JDOM API:-
JDOMReadXMLFIle.java
package com.gaurav.jdom.examples;
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 JDOMReadXMLFIle {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c://GeneratedXMLUsingJDOM.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootElement = document.getRootElement();
List elementList = rootElement.getChildren("employee");
for (int i = 0; i < elementList.size(); i++) {
Element nodeElement = (Element) elementList.get(i);
System.out.println("ID : " + nodeElement.getAttribute("id").getValue());
System.out.println("Name : " + nodeElement.getChildText("name"));
System.out.println("DESIGNATION : "
+ nodeElement.getChildText("designation"));
System.out.println("JAVA-EXPERIENCE : "
+ nodeElement.getChildText("java-experience"));
System.out.println("SALARY : " + nodeElement.getChildText("salary"));
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} catch (JDOMException jdomException) {
System.out.println(jdomException.getMessage());
}
}
}
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 JDOMReadXMLFIle {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c://GeneratedXMLUsingJDOM.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootElement = document.getRootElement();
List elementList = rootElement.getChildren("employee");
for (int i = 0; i < elementList.size(); i++) {
Element nodeElement = (Element) elementList.get(i);
System.out.println("ID : " + nodeElement.getAttribute("id").getValue());
System.out.println("Name : " + nodeElement.getChildText("name"));
System.out.println("DESIGNATION : "
+ nodeElement.getChildText("designation"));
System.out.println("JAVA-EXPERIENCE : "
+ nodeElement.getChildText("java-experience"));
System.out.println("SALARY : " + nodeElement.getChildText("salary"));
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} catch (JDOMException jdomException) {
System.out.println(jdomException.getMessage());
}
}
}
Result:-
ID : 1234
Name : KUMAR GAURAV
DESIGNATION : SYSTEM ENGINEER
JAVA-EXPERIENCE : 7
SALARY : 50000
ID : 2341
Name : KUMAR AADITYA
DESIGNATION : PROGRAM MANAGER
JAVA-EXPERIENCE : 15
SALARY : 7000000
Name : KUMAR GAURAV
DESIGNATION : SYSTEM ENGINEER
JAVA-EXPERIENCE : 7
SALARY : 50000
ID : 2341
Name : KUMAR AADITYA
DESIGNATION : PROGRAM MANAGER
JAVA-EXPERIENCE : 15
SALARY : 7000000
Note:- I am reading the same XML file which i generated using the previous program.
No comments:
Post a Comment