
- 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 removeChildren() Method
The Java JDOM removeChildren() method of Element class is used to remove child elements one level deep from the current element. Using this method, we can remove all child elements with the similar local name and within a specific namespace.
Syntax
Following is the syntax of the Java JDOM Element removeChildren() method −
Element.removeChildren(cname); Element.removeChildren(cname, ns);
Parameters
The Java removeChildren() method is a polymorphic method and it accepts the following parameters −
- cname − represents the local name of the child element to remove.
- ns − represents the namespace of the child element.
Return Value
The Java removeChildren() method returns the removed status in the form of a boolean variable.
Example 1
We need to parse the following stationary.xml file −
<stationary> <pre:pencil xmlns:pre = "https://namespace/pencil">Apsara</pre:pencil> <pencil>Nataraj</pencil> <book>Camlin</book> <pen>Cello</pen> </stationary>
Here is the basic example of using the Java JDOM Element removeChildren() method −
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class RemoveChildren { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("stationary.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Remove child elements Boolean removed = root.removeChildren("pencil"); System.out.println("removed? " + removed); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the removed status of the child elements.
removed? true
Example 2
The removeChildren() method returns false if the supplied local name of the child elements doesn't match with any of the local names of the children.
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class RemoveChildren { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("stationary.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Remove child elements Boolean removed = root.removeChildren("eraser"); System.out.println("removed? " + removed); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the removed status of the child elements.
removed? false
Example 3
The removeChildren() method can be used to remove the child elements within a specified namespace.
import java.io.File; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class RemoveChildren { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("stationary.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Remove child elements Namespace ns = Namespace.getNamespace("https://namespace/pencil"); root.removeChildren("pencil", ns); //Print document XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); xmlOutput.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); } } }
The XML document after removing the specific child element is displayed.
<?xml version="1.0" encoding="UTF-8"?> <stationary> <pencil>Nataraj</pencil> <book>Camlin</book> <pen>Cello</pen> </stationary>