- 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 removeAttribute() Method
The Java JDOM removeAttribute() method of Element class is used to remove attributes of an XML document. This method returns a boolean value after removing the attribute. We can remove attributes by passing Attribute object or local name of the attribute. Using this method, an attribute within a particular namespace can be removed.
Syntax
Following is the syntax of the Java JDOM Element removeAttribute() method −
Element.removeAttribute(attname); Element.removeAttribute(attname, ns); Element.removeAttribute(attribute);
Parameters
The Java removeAttribute() method is a polymorphic method and it accepts the following parameters −
- attname − the name of the attribute to be removed.
- ns − the namespace of the attribute that needs to be removed.
- attribute − the Attribute object of an XML Element.
Return Value
The Java removeAttribute() method returns true if attribute is removed; else returns false.
Example 1
We need to parse the following college.xml file −
<?xml version="1.0" encoding="UTF-8"?>
<department xmlns:buz ="https://attribute/ns/buz" buz:id="101" code="CS" id="90">
<name>Computer Science</name>
<staffCount>20</staffCount>
</department>
The following basic Java program uses the Java JDOM Element removeAttribute() method to remove an attribute.
import java.io.File;
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 RemoveAttribute {
public static void main(String args[]) {
try {
//Read the document and get the root
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File("college.xml");
Document doc = saxBuilder.build(inputFile);
Element root = doc.getRootElement();
//remove attribute
root.removeAttribute("id");
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The XML document after removing 'id' attribute is displayed.
<?xml version="1.0" encoding="UTF-8"?> <department xmlns:buz="https://attribute/ns/buz" buz:id="101" code="CS"> <name>Computer Science</name> <staffCount>20</staffCount> </department>
Example 2
The removeAttribute() method can be used to remove an attribute within a specific 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 RemoveAttribute {
public static void main(String args[]) {
try {
//Read the document and get the root
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File("college.xml");
Document doc = saxBuilder.build(inputFile);
Element root = doc.getRootElement();
//remove attribute
Namespace ns = Namespace.getNamespace("https://attribute/ns/buz");
root.removeAttribute("id",ns);
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The XML document after removing 'id' attribute with prefix 'buz' is displayed.
<?xml version="1.0" encoding="UTF-8"?> <department xmlns:buz="https://attribute/ns/buz" code="CS" id="90"> <name>Computer Science</name> <staffCount>20</staffCount> </department>
Example 3
The removeAttribute() method can used to remove attributes by passing the Attribute objects as arguments.
import java.io.File;
import java.util.List;
import org.jdom2.Attribute;
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 RemoveAttribute {
public static void main(String args[]) {
try {
//Read the document and get the root
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File("college.xml");
Document doc = saxBuilder.build(inputFile);
Element root = doc.getRootElement();
//remove attribute
List<Attribute> attrList = root.getAttributes();
root.removeAttribute(attrList.get(1));
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The XML document after removing 'code' attribute is displayed.
<?xml version="1.0" encoding="UTF-8"?> <department xmlns:buz="https://attribute/ns/buz" buz:id="101" id="90"> <name>Computer Science</name> <staffCount>20</staffCount> </department>