Java JDOM Document getNamespacesInherited() Method



The Java JDOM getNamespacesInherited() method of Document class is used to get all the namespaces that are inherited by the current XML document at the document level.

Syntax

Following is the syntax of the Java JDOM Document getNamespacesInherited() method −

Document.getNamespacesInScope();

Parameters

The Java getNamespacesInherited() method doesn't accept any parameters.

Return Value

The Java getNamespacesInherited() method returns the list of inherited Namespace objects.

Example

Following is the books.xml file we need to parse −

<?xml version="1.0" encoding="UTF-16" ?>
<book xmlns="http://domain/book">
    <pre:name xmlns:pre="http://domain/bookName">
       War and Peace
    </pre:name>
</book>

The getNamespacesInherited() method returns an empty list when there are no namespaces inherited by the XML document.

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


public class NamespacesInherited {
   public static void main(String args[]) {
      try {  	  
         //Reading the XML file
		 SAXBuilder saxBuilder = new SAXBuilder();
		 File inputFile = new File("books.xml");			          
		 Document doc = saxBuilder.build(inputFile);
		 System.out.println(doc.getNamespacesInherited());		 
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays an empty list.

[]
Advertisements