
- 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 removeContent() Method
The Java JDOM removeContent() method of Element class is used to remove content objects inside an XML element. These content objects include comments, text and child elements etc. Using this method, any content object at a specified index can be removed. Content objects of a specified type can also be removed using this method.
Syntax
Following is the syntax of the Java JDOM Element removeContent() method −
Element.removeContent(); Element.removeContent(child); Element.removeContent(filter); Element.removeContent(index);
Parameters
The Java removeContent() method is a polymorphic method and it accepts the following parameters −
- child − represents a content object that needs to be removed.
- filter − represents a filter object of a specific content type.
- index − represents the index of content object that needs to be removed.
Return Value
The Java removeContent() method returns the removed content objects.
Example 1
We need to parse the following sample.xml file −
<!-- Root information is available here --> <root> I'm the root <!-- This root has three children --> <child>First child</child> <child>Second child</child> </root>
Here is the basic example of using the Java JDOM Element removeContent() method −
import java.io.File; import java.util.List; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class RemoveContent { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("sample.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Remove content List<Content> conList = root.removeContent(); System.out.println(conList); } catch (Exception e) { e.printStackTrace(); } } }
The removed list of content objects are displayed.
[[Text: I'm the root ], [Comment: <!-- This root has three children -->], [Text: ], [Element: <child/>], [Text: ], [Element: <child/>], [Text: ]]
Example 2
The removeContent() method can be used to remove content object at a specified index by passing the index as an argument.
import java.io.File; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class RemoveContent { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("sample.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Remove content Content con = root.removeContent(0); System.out.println(con); } catch (Exception e) { e.printStackTrace(); } } }
The removed text content object is displayed.
[Text: I'm the root ]
Example 3
The removeContent() method can be used to remove specific content objects by passing the filter object as an argument to it.
import java.io.File; import java.util.List; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.filter.Filter; import org.jdom2.filter.Filters; import org.jdom2.input.SAXBuilder; public class RemoveContent { public static void main(String args[]) { try { //Reading the document and get the root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("sample.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Remove only elements Filter<Element> element_filter = Filters.element(); List<Element> elementList = root.removeContent(element_filter); for(Content con : elementList) { System.out.println(con); } } catch(Exception e) { e.printStackTrace(); } } }
The output window displays the removed child elements.
[Element: <child/>] [Element: <child/>]