Saturday, December 31, 2011

XML parsing in java example using JDOM


JDOM provides a very straightforward mechanism of parsing an XML file. JDOM does not itself include a parser. Instead it depends on a SAX parser to parse documents and build JDOM models from them. Once, the model document is built, the any element can be easily accessed through appropriate methods.

The following sample code parses the file "Personnel.xml" and produces the console output as shown below :

# Personnel.xml

<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
    <Employee type="permanent" id="p124">
         <Name>Manoj Shrestha</Name>
         <Age>25</Age>
         <Address><![CDATA[Tokyo - Shinjuku 1-152-21-533]]></Address>
    </Employee>
    <Employee type="contract" id="c230">
         <Name>Lionel Messi</Name>
         <Age>24</Age>
         <Address><![CDATA[Barcelona 444-23]]></Address>
    </Employee>
</Personnel>

Source Code :


package com.manoj.examples.xml;

import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * The Class XMLParserJDOM.
 * @author Manoj Shrestha
 * @Contact m.javaprogrammer@gmail.com
 */
public class XMLParserJDOM {
      public static void main(String[] args) {
            try {
                  // The file name is relative to the main project folder
                  Document document = new SAXBuilder().build("Personnel.xml");
                  Element rootElement = document.getRootElement();

                  System.out.println("Root Element : " + rootElement.getName());
                  System.out.println();

                  List<Element> employeeList = rootElement.getChildren("Employee");
                  for (Element employee : employeeList) {
                        System.out.print("Employee Details : ");
                        System.out.print(" Id = " + employee.getAttributeValue("id"));
                        System.out.print(" Type = " + employee.getAttributeValue("type"));
                        System.out.print(" Name = " + employee.getChild("Name").getText());
                        System.out.print(" Age = " + employee.getChild("Age").getText());
                        System.out.println(" Address = " 
                                + employee.getChild("Address").getText());
                  }
            } catch (JDOMException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            }
      }
}



# The console output :


Root Element : Personnel

Employee Details :  Id = p124 Type = permanent Name = Manoj Shrestha Age = 25 Address = Tokyo - Shinjuku 1-152-21-533
Employee Details :  Id = c230 Type = contract Name = Lionel Messi Age = 24 Address = Barcelona 444-23










XML parsing in java example using SAX


SAX parsing is event based modelling.When a Sax parser parses a XML document,  it generates call back events as it encounters start-tags, end-tags, text, comments and so on. The class DefaultHandler is the base class to listen such events. It contains methods to handle respective events. For example, 

when it encounters a Start Tag it calls method
    public void startElement()
when it encounters a End Tag it calls this method
    public void endElement()


Source Code : 
package com.manoj.examples.xml;

import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * The Class XMLParserSAX.
 * @author Manoj Shrestha
 * @contact m.javaprogrammer@gmail.com
 */
public class XMLParserSAX extends DefaultHandler {
      private static final String QUOTE = "\"";

      public static void main(String args[]) throws Exception {
            try {
                  // get a new instance of parser
                  SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
                  // parse the file and also register this class for call backs
                  // The file name is relative to the main project folder
                  sp.parse("Personnel.xml", new XMLParserSAX());

            } catch (ParserConfigurationException e) {
                  e.printStackTrace();
            } catch (SAXException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            }
           
            // Another way to parse XML through XMLReader
            /**
                  FileReader fileReader = new FileReader("Personnel.xml");
                  XMLReader xr = XMLReaderFactory.createXMLReader();
                  xr.setContentHandler(new XMLParserSAX());
                  xr.parse(new InputSource(fileReader));
            **/
      }

      @Override
      public void startDocument() {
            System.out.println("Start document");
      }

      @Override
      public void endDocument() {
            System.out.println("End document");
      }

      @Override
      public void startElement(String uri, String name, String qName,
                  Attributes atts) {
            if ("".equals(uri))
                  System.out.print("Start element: " + qName);
            else
                  System.out.print("Start element: {" + uri + "}" + name);

            if (atts.getLength() > 0) {
                  System.out.print(" Attributes : ");
                  for (int i = 0; i < atts.getLength(); i++) {
                        System.out.print(atts.getLocalName(i) + " = " + QUOTE
                                    + atts.getValue(i) + QUOTE + " ");

                  }
            }
            System.out.println();
      }

      @Override
      public void endElement(String uri, String name, String qName) {
            if ("".equals(uri))
                  System.out.println("End element: " + qName);
            else
                  System.out.println("End element:   {" + uri + "}" + name);
      }

      @Override
      public void characters(char ch[], int start, int length) {
            System.out.print("Characters:    \"");
            for (int i = start; i < start + length; i++) {
                  switch (ch[i]) {
                  case '\\':
                        System.out.print("\\\\");
                        break;
                  case '"':
                        System.out.print("\\\"");
                        break;
                  case '\n':
                        System.out.print("\\n");
                        break;
                  case '\r':
                        System.out.print("\\r");
                        break;
                  case '\t':
                        System.out.print("\\t");
                        break;
                  default:
                        System.out.print(ch[i]);
                        break;
                  }
            }
            System.out.print("\"\n");
      }
}




The input file : “Personnel.xml”
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
      <Employee type="permanent" id="p124">
            <Name>Manoj Shrestha</Name>
            <Age>25</Age>
            <Address>Tokyo</Address>
      </Employee>
      <Employee type="contract" id="c230">
            <Name>Lionel Messi</Name>
            <Age>24</Age>
            <Address>Barcelona</Address>
      </Employee>
</Personnel>

The console output :
Start document
Start element: Personnel
Characters:    "\n\t"
Start element: Employee Attributes : type = "permanent" id = "p124"
Characters:    "\n\t\t"
Start element: Name
Characters:    "Manoj Shrestha"
End element: Name
Characters:    "\n\t\t"
Start element: Age
Characters:    "25"
End element: Age
Characters:    "\n\t\t"
Start element: Address
Characters:    "Tokyo"
End element: Address
Characters:    "\n\t"
End element: Employee
Characters:    "\n\t"
Start element: Employee Attributes : type = "contract" id = "c230"
Characters:    "\n\t\t"
Start element: Name
Characters:    "Lionel Messi"
End element: Name
Characters:    "\n\t\t"
Start element: Age
Characters:    "24"
End element: Age
Characters:    "\n\t\t"
Start element: Address
Characters:    "Barcelona"
End element: Address
Characters:    "\n\t"
End element: Employee
Characters:    "\n"
End element: Personnel
End document