
- 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 Document getNamespacesInScope() Method
The Java JDOM getNamespacesInScope() method of Document class is used to get all the namespaces that are in scope of the current XML document. This method returns the introduced namespaces and the inherited namespaces of the XML document at the document level.
Syntax
Following is the syntax of the Java JDOM Document getNamespacesInScope() method −
Document.getNamespacesInScope();
Parameters
The Java getNamespacesInScope() method doesn't accept any parameters.
Return Value
The Java getNamespacesInScope() method returns the list of in-scope Namespace objects.
Example 1
Here is the basic example of using the Java JDOM Document getNamespacesInScope() method −
import java.util.List; import org.jdom2.Document; import org.jdom2.Namespace; public class NamespacesInScope { public static void main(String args[]) { try { //Create a new document Document doc = new Document(); //Get In-scope Namespaces List<Namespace> ns = doc.getNamespacesInScope(); for(Namespace namespace : ns) { System.out.println(namespace); } } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the namespaces in scope.
[Namespace: prefix "" is mapped to URI ""] [Namespace: prefix "xml" is mapped to URI "http://www.w3.org/XML/1998/namespace"]
Example 2
We need to parse the following books.xml file −
<?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 Document.getNamespacesInScope() method only gives the namespaces at document level. Though the root element and child elements have namespaces, this method will not retrieve them.
import java.io.File; import java.util.List; import org.jdom2.Document; import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; public class NamespacesInScope { 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); //Get In-scope Namespaces List<Namespace> ns = doc.getNamespacesInScope(); for(Namespace namespace : ns) { System.out.println(namespace); } } catch (Exception e) { e.printStackTrace(); } } }
The document level namespaces are displayed on the output screen.
[Namespace: prefix "" is mapped to URI ""] [Namespace: prefix "xml" is mapped to URI "http://www.w3.org/XML/1998/namespace"]