Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.4. Reading XML files

3.4. Reading XML files
Last updated: 7 February 2013.

This tutorial explains how to read XML files with JDOM. While reading this page, you might want to take a look at the JDOM javadoc.

The recommended way to read an XML file with JDOM is by using a SAX parser. The code below shows how to read an XML file named hotel.xml with a SAX parser:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;


public final class JDOMDemo {


   
private void readXmlFile(){
         
try {
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Do something with the document
                // ...
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readXmlFile();
   
}

}

The code is quite simple. First, an instance of the class SAXBuilder is created with the default constructor. Next, the method build is called to get a Document object containing the XML tree structure. The next tutorials will show you what you can do with the Document object.


Reading XML files with DTD validation

When reading the XML file, if you want to validate it against a DTD file, use the constructor SAXBuilder(XMLReaderJDOMFactory readersouce) like this:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;


public final class JDOMDemo {


   
private void readXmlFileWithDtdValidation(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder(XMLReaders.DTDVALIDATING);
                Document document = builder.build
(new File("hotel.xml"));

               
// Do something with the document
                // ...
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readXmlFileWithDtdValidation();
   
}

}

The DTD file must be named hotel.dtd and it must be in the same directory as the file hotel.xml.



Reading XML files with XSD validation

In the same manner, when reading the XML file, you can validate it against an XSD as follows:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;


public final class JDOMDemo {


   
private void readXmlFileWithXsdValidation(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
                Document document = builder.build
(new File("hotel.xml"));

               
// Do something with the document
                // ...
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readXmlFileWithXsdValidation();
   
}

}

Of course, there must be a file named hotel.xsd located in the same directory as the file hotel.xml.


Reading XML from a string value

In some situations, you might need to read XML from a string value. In that case, use the method build(java.io.Reader characterStream) provided by the class SAXBuilder like this:

Document document = builder.build(new StringReader(stringValue));


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.4. Reading XML files
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.5. Outputting XML from Java code

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7