Java DOM4J Parser – Create XML Document
”;
Java DOM4J library has classes, interfaces and methods to create XML documents. The interfaces Document and Element of org.dom4j package has methods to create XML documents. The XMLWriter class of org.dom4j.io package writes the XML content of the document into the file. In this chapter we are going to use these interfaces and classes to create XML files.
Create XML Using Java DOM4J Parser
Following are the steps to create XML documents using Java DOM4J Parser −
- Step 1: Creating a new Document
- Step 2: Creating and Adding XML elements
- Step 3: Creating a FileOutputStream
- Step 4: Writing the XML document into file
- Step 5: Printing the XML document
Step 1: Creating a new Document
The DocumentHelper class of org.dom4j package provides the helper methods to use DOM4J. The createDocument() method creates a new document as follows −
Document document = DocumentHelper.createDocument();
Step 2: Creating and Adding XML elements
We can create root element and add it to the XML document using addElement() method. Also, we can add child elements to the root element using the same addElement() method. To add attributes, we use addAttribute() method.
Element root = document.addElement( "root_ele_name" );
Element child = root.addElement("child_ele_name").addAttribute("attr_name", "attr_val");
Step 3: Creating a FileOutputStream
To copy the content of XML document into a file, we need to create a FileOutputStream as follows −
FileOutputStream newFile = new FileOutputStream("D://newDOM4jFile.xml");
Step 4: Writing the XML document into file
The XMLWriter class has write() method to write the XML document into the specified file. We can also send the format we want to add to the content as second argument to the write() method.
XMLWriter writer = new XMLWriter(newFile);
writer.write( document );
Step 5: Printing the XML document
To print the content of XML document, we can pass System.out as the first argument and format as the second argument. The createPrettyPrint() method creates proper indentation of XML elements while displaying. This step is optional and can be used for testing purpose.
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter consoleWriter = new XMLWriter( System.out, format );
consoleWriter.write( document );
Creating XML file
The addElement() method can be used with either Document or Element interface. This method takes name of the element as a parameter and adds it to the specified document or element. To add root element, we use Document.addElement(root_ele_name).
The addAttribute() method takes name of the attribute as first argument and value as the second argument. It adds the attribute to the specified element.
The addText() method adds the text content to the element. It takes the text content as a String and adds to the corresponding element.
Example
Using the methods discussed above, we are going to create an XML file with ”cars” as root element and company as its attribute. Let us add two ”carname” elements with ”type” as attribute.
We need to create the following cars.xml file −
<?xml version = "1.0" encoding = "UTF-8"?>
<cars>
<supercars company = "Ferrari">
<carname type = "formula one">Ferrari 101</carname>
<carname type = "sports">Ferrari 202</carname>
</supercars>
</cars>
The following CreateXMLDemo.java program creates a document and adds the elements using addElement() method and attributes using addAttribute() method. To set the text content, addText() method is used. The XML content of the document is transferred into a file and also printed on the console.
package java_xml_dom4j;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class CreateDemo {
public static void main(String[] args) {
try {
//Creating a new Document
Document document = DocumentHelper.createDocument();
//Creating and Adding XML elements
Element root = document.addElement( "cars" );
Element supercarElement = root.addElement("supercars")
.addAttribute("company", "Ferrai");
supercarElement.addElement("carname")
.addAttribute("type", "Ferrari 101")
.addText("Ferrari 101");
supercarElement.addElement("carname")
.addAttribute("type", "sports")
.addText("Ferrari 202");
//Creating a FileOutputStream
FileOutputStream newFile = new FileOutputStream("D://newDOM4jFile.xml");
//Writing the XML document into file
XMLWriter writer = new XMLWriter(newFile);
writer.write( document );
//Printing the XML document
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter consoleWriter = new XMLWriter( System.out, format );
consoleWriter.write( document );
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
The output window displays the XML content as follows −
<?xml version = "1.0" encoding = "UTF-8"?>
<cars>
<supercars company = "Ferrari">
<carname type = "formula one">Ferrari 101</carname>
<carname type = "sports">Ferrari 202</carname>
</supercars>
</cars>