
- 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 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)