Java JDOM Element removeNamespaceDeclaration() Method



The Java JDOM removeNamespaceDeclaration() method of Element class is used to remove additional namespaces associated with the XML Element. This method can be used to remove namespaces with certain prefix when they are not needed inside the element. If there are no Namespaces for an Element, this method does no alteration to the original XML document.

Syntax

Following is the syntax of the Java JDOM Element removeNamespaceDeclaration() method −

Element.removeNamespaceDeclaration(namespace);

Parameters

The Java removeNamespaceDeclaration() method accepts a single parameter.

namespace − additional Namespace that we need to remove.

Return Value

The Java removeNamespaceDeclaration() method has no return value.

Example 1

Here is the sample.xml file that has two namespaces for the root.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:pre1="https://namespaces/root1" xmlns:pre2="https://namespaces/root2" >I'm root.</root>

Using the Java JDOM Element removeNamespaceDeclaration() method, we can remove the additional namespace as follows −

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 RemoveNSDeclaration {
   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 nameSpace declaration
	     root.removeNamespaceDeclaration(root.getNamespace("pre2"));
	     //print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The document after removing additional namespace is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:pre1="https://namespaces/root1">I'm root.</root>

Example 2

The removeNamespaceDeclaration() method does nothing if there is no namespace declaration associated with the XML Element.

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class RemoveNSDeclaration {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
	     Document doc = new Document();
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Remove namespace
	     root.removeNamespaceDeclaration(root.getNamespace());
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The document is displayed without any modification.

<?xml version="1.0" encoding="UTF-8"?>
<root>I'm root.</root>
Advertisements