Java JDOM Document getBaseURI() Method



The Java JDOM getBaseURI() method of Document class is used to get the URI from which the XML document gets loaded. If the fetch location of the document is unknown, it returns null.

Syntax

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

Document.getBaseURI();

Parameters

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

Return Value

The Java getBaseURI() method returns a String value that represents the BaseURI.

Example 1

The following basic example uses the Java JDOM Document getBaseURI() method to get the file location.

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

public class GetBaseURI {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("D://cars.xml");
 		 Document doc = saxBuilder.build(inputFile);		
 		 //Get BaseURI
	     String baseURI =  doc.getBaseURI();
	     System.out.println("BaseURI : "+baseURI);	      	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the BaseURI of the XML document.

BaseURI : file:/D:/cars.xml

Example 2

The getBaseURI() method returns null if the file location is unknown. The following java program illustrates this scenario.

import org.jdom2.Document;

public class GetBaseURI {
   public static void main(String args[]) {
      try {	
    	 //Creating new Document
 		 Document doc = new Document();
 		 //Get BaseURI
	     String baseURI =  doc.getBaseURI();
	     System.out.println("BaseURI : "+baseURI);   	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays null.

BaseURI : null
Advertisements