DOM
Remove attribute from DOM element
With this example we are going to demonstrate how to remove an attribute from a DOM Element. We have implemented a method, that is void prettyPrint(Document xml)
, in order to convert a DOM into a formatted XML String. In short, to remove an attribute from a DOM Element you should:
- Obtain a new instance of a DocumentBuilderFactory, that is a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
- Set the parser produced so as not to validate documents as they are parsed, using
setValidating(boolean validating)
API method of DocumentBuilderFactory, with validating set to false. - Create a new instance of a DocumentBuilder, using
newDocumentBuilder()
API method of DocumentBuilderFactory. - Parse the FileInputStream with the content to be parsed, using
parse(InputStream is)
API method of DocumentBuilder. This method parses the content of the given InputStream as an XML document and returns a new DOM Document object. - Get the NodeList of all the Element objects in document order with a given tag name and are contained in the document using
getElementsByTagName(String tagname)
API method of Document and from this nodeList get the first element. - Check if a specified attribute is set in the element, using
hasAttribute(Sting name)
API method of Element. - Remove the specified attribute from the element, using
removeAttribute(String name)
API method of Element. - Call
void prettyPrint(Document xml)
method of the example. The method gets the xml Document and converts it into a formatted xml String, with specific parameters, such as encoding. The method uses a Transformer, that is created usingnewTransformer()
API method of TransformerFactory. The Transformer is used to transform a source tree into a result tree. After setting specific output properties to the transformer, usingsetOutputProperty(String name, String value)
API method of Transformer, the method uses it to make the transformation, withtransform(Source xmlSource, Result outputTarget)
API method of Transformer. The parameters are the DOMSource with the DOM node and the result that is a StreamResult created from a StringWriter.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import java.io.File; import java.io.FileInputStream; import java.io.StringWriter; import java.io.Writer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class RemoveAttributeFromDOMElement { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new FileInputStream(new File("in.xml"))); Element element = (Element) doc.getElementsByTagName("channel").item(0); // whether an attribute with a given name is specified on this element or has a default value boolean hasAttribute = element.hasAttribute("premium"); System.out.println("Has Attribute: " + hasAttribute); System.out.println("Attribute Value: " + element.getAttribute("premium")); System.out.println(); // Removes an attribute by name. If a default value for the removed attribute // is defined in the DTD, a new attribute immediately appears with the default value element.removeAttribute("premium"); prettyPrint(doc); } public static final void prettyPrint(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); System.out.println(out.toString()); } }
Input:
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel premium="premiumchannel"> <title>Java Tutorials and Examples</title> <item> <title><![CDATA[Java Tutorials]]></title> <link>http://www.javacodegeeks.com/</link> </item> <item> <title><![CDATA[Java Examples]]></title> <link>http://examples.javacodegeeks.com/</link> </item> </channel> </rss>
Output:
Has Attribute: true
Attribute Value: premiumchannel
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<rss version="2.0">
<channel>
<title>Java Tutorials and Examples</title>
<item>
<title><![CDATA[Java Tutorials]]></title>
<link>http://www.javacodegeeks.com/</link>
</item>
<item>
<title><![CDATA[Java Examples]]></title>
<link>http://examples.javacodegeeks.com/</link>
</item>
</channel>
</rss>
This was an example of how to remove an attribute from a DOM Element in Java.