JavaPerspective.com >
Advanced Tutorials >
3. XML processing with JDOM >
3.7. Creating XML from Java code
3.7. Creating XML from Java code
Last updated: 2 January 2013.
This tutorial will show you how to create a JDOM document from Java code. It is actually quite simple. First, you need to create the root element to which you add children as needed. Then you create a Document object with the root element. Here is an example:
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public final class JDOMDemo {
private void createNewDocument(){
try {
// Create the root element
Element rootElement = new Element("HOTEL");
// Create a new ROOM element
Element room = new Element("ROOM");
// Set up a new NUMBER element and add it to ROOM
Element number = new Element("NUMBER");
number.addContent("17");
room.addContent(number);
// Set up a CATEGORY element and add it to ROOM
Element category = new Element("CATEGORY");
category.addContent("STANDARD");
room.addContent(category);
// Set up a BED element and add it to ROOM
Element bed = new Element("BED");
bed.addContent("DOUBLE SIZE");
room.addContent(bed);
// Set up a FLOOR element and add it to ROOM
Element floor = new Element("FLOOR");
floor.addContent("5");
room.addContent(floor);
// Set up an AIR_CONDITIONING element and add it to ROOM
Element airConditioning = new Element("AIR_CONDITIONING");
airConditioning.addContent(new Element("TABLE_FANS").addContent("2"));
room.addContent(airConditioning);
// Add the newly created ROOM element to the root element
rootElement.addContent(room);
// Create the document with the root element
Document document = new Document(rootElement);
// Output the JDOM document to the standard output
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(document, System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new JDOMDemo().createNewDocument();
}
}
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public final class JDOMDemo {
private void createNewDocument(){
try {
// Create the root element
Element rootElement = new Element("HOTEL");
// Create a new ROOM element
Element room = new Element("ROOM");
// Set up a new NUMBER element and add it to ROOM
Element number = new Element("NUMBER");
number.addContent("17");
room.addContent(number);
// Set up a CATEGORY element and add it to ROOM
Element category = new Element("CATEGORY");
category.addContent("STANDARD");
room.addContent(category);
// Set up a BED element and add it to ROOM
Element bed = new Element("BED");
bed.addContent("DOUBLE SIZE");
room.addContent(bed);
// Set up a FLOOR element and add it to ROOM
Element floor = new Element("FLOOR");
floor.addContent("5");
room.addContent(floor);
// Set up an AIR_CONDITIONING element and add it to ROOM
Element airConditioning = new Element("AIR_CONDITIONING");
airConditioning.addContent(new Element("TABLE_FANS").addContent("2"));
room.addContent(airConditioning);
// Add the newly created ROOM element to the root element
rootElement.addContent(room);
// Create the document with the root element
Document document = new Document(rootElement);
// Output the JDOM document to the standard output
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(document, System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new JDOMDemo().createNewDocument();
}
}
The output is:
<?xml version="1.0" encoding="UTF-8"?>
<HOTEL>
<ROOM>
<NUMBER>17</NUMBER>
<CATEGORY>STANDARD</CATEGORY>
<BED>DOUBLE SIZE</BED>
<FLOOR>5</FLOOR>
<AIR_CONDITIONING>
<TABLE_FANS>2</TABLE_FANS>
</AIR_CONDITIONING>
</ROOM>
</HOTEL>
<HOTEL>
<ROOM>
<NUMBER>17</NUMBER>
<CATEGORY>STANDARD</CATEGORY>
<BED>DOUBLE SIZE</BED>
<FLOOR>5</FLOOR>
<AIR_CONDITIONING>
<TABLE_FANS>2</TABLE_FANS>
</AIR_CONDITIONING>
</ROOM>
</HOTEL>
You are here :
JavaPerspective.com >
Advanced Tutorials >
3. XML processing with JDOM >
3.7. Creating XML from Java code
Next tutorial : JavaPerspective.com > Advanced Tutorials > 3. XML processing with JDOM > 3.8. Retrieving nodes content
Next tutorial : JavaPerspective.com > Advanced Tutorials > 3. XML processing with JDOM > 3.8. Retrieving nodes content