
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
Java JDOM Document addContent() Method
The Java JDOM addContent() method of Document class is used to add content to an XML document. Using this method, we can add root element, comments and processing instruction to the document.
This method adds the content at the end of the document if index value is not provided. If index value is used, it appends the content object or content list at the specified position. If a negative index or number greater than the existing children of document is passed, it throws IndexOutOfBoundsException. If the content object we are trying to add already has a parent, then it throws IllegalAddException.
Syntax
Following is the syntax of the Java JDOM Document addContent() method −
Document.addContent(child) Document.addContent(index, child) Document.addContent(list) Document.addContent(index,list)
Parameters
The Java addContent() method is a polymorphic method and has different parameters for each of them.
child − represents a Content object. This can be an Element, Comment, DocType, EntityRef, ProcessingInstruction and Text.
index − represents the index at which we need to insert the Content or list of Content objects.
list − represents the list of Content objects.
Return Value
This method returns the Document on which the content gets added.
Example 1
Here is the basic example that uses Java JDOM Document addContent() method to add root Element to an XML document.
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.XMLOutputter; public class AddRootElement { public static void main(String args[]) { try { //Create a new Document Document doc = new Document(); //Create root Element Element root = new Element("company").setText("xyz"); //Add root Element doc.addContent(root); //Print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the XML document having a root element with text content.
<?xml version="1.0" encoding="UTF-8"?> <company>xyz</company>
Example 2
Now, let us try to add one more element in the same level as the root element and this throws an exception named, IllegalAddException. The following example illustrates this scenario.
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.XMLOutputter; public class AddRootElement { public static void main(String args[]) { try { //Adding root to new Document Document doc = new Document(); Element root = new Element("company").setText("xyz"); //Trying to add one more root doc.addContent(0,root); Element root2 = new Element("footer"); doc.addContent(1,root2); //Print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
org.jdom2.IllegalAddException: Cannot add a second root element, only one is allowed
Example 3
We need to add comment at the end of the EmpData.xml file below−
<Company> <Employee>John</Employee> <Employee>Daniel</Employee> </Company>
To add comment at the end of the document, in the following example, we have created a new Comment object and used the addContent(Comment) method and added it to the XML document.
import java.io.File; import org.jdom2.Comment; import org.jdom2.Document; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class AddComment { public static void main(String args[]) { try { //Reading the XML file SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("EmpData.xml"); Document doc = saxBuilder.build(inputFile); //Adding comment at the end Comment comment = new Comment("You have reached the end of the document!"); doc.addContent(comment); //Printing the document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the content of the document along with the comment at the end.
<?xml version="1.0" encoding="UTF-8"?> <Company> <Employee>John</Employee> <Employee>Daniel</Employee> </Company> <!--You have reached the end of the document!-->
Example 4
The method addContent(List<Content>) adds the content list at the end of the document. The following program creates a new document and adds the content list that contains comment and root element.
import java.util.ArrayList; import java.util.List; import org.jdom2.Comment; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class AddContent { public static void main(String args[]) { try { Document doc = new Document(); //Preparing the content list Comment comment = new Comment("This is the start of the document!"); Element root = new Element("company").setText("xyz"); List<Content> list = new ArrayList<>(); list.add(comment); list.add(root); //Adding the list doc.addContent(list); //Print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the content of the XML document.
<?xml version="1.0" encoding="UTF-8"?> <!--This is the start of the document!--> <company>xyz</company>