Java JDOM Element addNamespaceDeclaration() Method



The Java JDOM addNamespaceDeclaration() method of Element class adds the namespace for an XML Element. This method returns true upon successfully adding the namespace to the element. If the namespace is already present, it returns false. It throws an IllegalAddException when a namespace with already existing prefix gets added.

Syntax

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

Element.addNamespaceDeclaration(namespace);

Parameters

The Java addNamespaceDeclaration() method accepts a single parameter.

namespace − represents a Namespace object that needs to be added.

Return Value

The Java addNamespaceDeclaration() method returns a boolean value; false if namespace already present, true otherwise.

Example 1

The following basic example uses the Java JDOM Element addNamespaceDeclaration() method to add namespace to the root.

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

public class AddNamespace {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document
	     Document doc = new Document();
	     //Create and add root
	     Element root = new Element("root").setText("I'm root. ");
	     doc.setRootElement(root);
	     //Create a nameSpace
	     Namespace ns = Namespace.getNamespace("prefix","https://namespaces/root");
	     //Add nameSpace
	     root.addNamespaceDeclaration(ns);
	     //Print the document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);       
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The document after adding namespace to the root is displayed.

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

Example 2

The addNamespaceDeclaration() method returns false if the namespace that we are trying to add is already declared.

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;

public class AddNamespace {
   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();
	     //Create a nameSpace
	     Namespace ns = Namespace.getNamespace("prefix","https://namespaces/root");
	     //Add nameSpace
	     Boolean status = root.addNamespaceDeclaration(ns);  
	     System.out.println(status);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window returns false.

false

Example 3

The addNamespaceDeclaration() method throws an IllegalAddException when trying to add namespace with same prefix.

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;

public class AddNamespace {
   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();
	     //Create a nameSpace
	     Namespace ns = Namespace.getNamespace("prefix","https://namespaces/info");
	     //Add nameSpace
	     root.addNamespaceDeclaration(ns);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the error.

org.jdom2.IllegalAddException: The namespace xmlns:prefix="https://namespaces/info" could not be added as a namespace to "root": The namespace prefix "prefix" collides with an additional namespace declared by the element
	at org.jdom2.Element.addNamespaceDeclaration(Element.java:400)
	at addNSdeclaration.Example3.main(Example3.java:20)
Advertisements