Java JDOM Element setNamespace() Method



The Java JDOM setNamespace() method of Element class is used to set the namespace of an XML element. If the XML element doesn't have any namespace, the supplied namespace gets added, else the already existing namespace gets replaced. This method throws IllegalAddException, if there is a namespace conflict.

Syntax

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

Element.setNamespace(ns);

Parameters

The Java Element.setNamespace() method accepts a single parameter −

ns − represents the namespace object to be added.

Return Value

The Java setNamespace() method returns the XML Element after adding namespace.

Example 1

Here is the basic example of using the Java JDOM Element setNamespace() method −

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

public class SetNamespace {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
    	 Document doc = new Document();
	     Element root = new Element("book");
         //Set namespace	     
	     Namespace ns = Namespace.getNamespace("https://namespace/bookNS");
	     root.setNamespace(ns);
	     doc.setRootElement(root);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML document after setting the namespace for the root is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="https://namespace/bookNS" />

Example 2

The setNamespace() method sets no namespace when null is passed as an argument to it.

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

public class SetNoNamespace {
   public static void main(String args[]) {
      try {	
    	 //Create Document and add root
    	 Document doc = new Document();
	     Element root = new Element("book");
         //Set namespace	     
	     root.setNamespace(null);
	     doc.setRootElement(root);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The XML document after modification is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<book />

Example 3

We need to parse the following sample.xml file −

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="https://namespace/bookNS" />

The setNamespace() method replaces the already existing namespace with the supplied 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 UpdateNamespace {
   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();
		 //Set namespace
    	 Namespace ns = Namespace.getNamespace("https://namespace/newBookNS");
    	 root.setNamespace(ns);
	     //print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The updated XML document after modifying the namespace is displayed.

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="https://namespace/newBookNS" />
Advertisements