
- 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 Element addContent() Method
The Java JDOM addContent() method of Element class is used to add content objects inside an XML Element. These content objects include text, elements and comments. Only legal content objects are allowed. Content objects such as processing instruction, DocType declaration are not allowed inside an XML Element.
The addContent() method throws an illegalAddException if parent already exists for the content object that we are trying to add to the current element or it is an illegal content object. This exception is also thrown when incorrect index supplied while adding the content.
Syntax
Following is the syntax of the Java JDOM Element addContent() method −
Element.addContent(str); Element.addContent(child); Element.addContent(list); Element.addContent(index, child); Element.addContent(index, list);
Parameters
The Java addContent() method is a polymorphic method and accepts the following parameters −
- str − text content to be added to the Element.
- child − child content object that needs to be added.
- list − list of content objects to be added.
- index − index at which the content needs to be added.
Return Value
The Java addContent() method returns the Element object after adding the content.
Example 1
Here is the basic example of using the Java JDOM Element addContent() method −
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class AddChildElement { public static void main(String args[]) { try { //Create a new Document Document doc = new Document(); //Create and add root Element root = new Element("root").setText("I'm root. "); doc.setRootElement(root); //Create a child element Element child = new Element("child"); //Add element root.addContent(child); //Print the document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The updated document after adding the child element is displayed.
<?xml version="1.0" encoding="UTF-8"?> <root> I'm root. <child /> </root>
Example 2
The addContent() method takes string as an argument and adds as a text content to the Element object. If text content is already present, it appends the supplied string to the existing text content.
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class AddTextContent { public static void main(String args[]) { try { //Create a new Document Document doc = new Document(); //Create and add root Element root = new Element("root").setText("I'm root. "); doc.setRootElement(root); //Create and add text content String textContent = "Extra text content added."; root.addContent(textContent); //Print the document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The updated document after adding the text content is displayed.
<?xml version="1.0" encoding="UTF-8"?> <root>I'm root. Extra text content added.</root>
Example 3
By default, the addContent() method inserts the content list or content object at the end of the already existing content list. To insert content at a specified position, an index must be passed as first argument.
import java.io.File; import org.jdom2.Comment; import org.jdom2.Document; import org.jdom2.Element; 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 document and get root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("sample.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Create comment Comment comment = new Comment("Here is the info of child..."); //Add comment at index 0 root.addContent(0,comment); //Print the document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The updated document after adding the comment is displayed.
<?xml version="1.0" encoding="UTF-8"?> <root> <!--Here is the info of child...--> <child /> </root>
Example 4
The addContent() method can also be used to add the list of different content objects to an XML 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 AddContentList { public static void main(String args[]) { try { //Create a new Document and add root Document doc = new Document(); Element root = new Element("root"); doc.setRootElement(root); //Create child objects Comment comment = new Comment("Here is the info of children..."); Element child1 = new Element("child1"); Element child2 = new Element("child2"); //create list and add content objects List<Content> list = new ArrayList<>(); list.add(comment); list.add(child1); list.add(child2); //Add content root.addContent(list); //Print the document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The updated document after adding the content list is displayed.
<?xml version="1.0" encoding="UTF-8"?> <root> <!--Here is the info of children...--> <child1 /> <child2 /> </root>