Thursday, 19 May 2011

XMLEventReader

XMLEventReader provides the ability to peek at the next event and returns configuration information through the property interface.

Here is a sample employee feed which we will parse using XMLEventReader:


<employeefeed>
 <employees>
  <employee>
   <firstname>Anuja</firstname>
   <lastname>Kulkarni</lastname>
   <department>Development</department>
   <designation>J2EE Designer</designation>
  </employee>
  <employee>
   <firstname>Ruby</firstname>
   <lastname>Kulkarni</lastname>
   <department>HR</department>
   <designation>Recruitment</designation>
  </employee>  
 </employees>
</employeefeed>


And here is the parser:


package com.jbasics;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;

public class CustomXMLEventReader {
 private static final String EMPLOYEE = "Employee";
 private static final String FIRSTNAME = "FirstName";
 private static final String LASTNAME = "LastName";
 private static final String DEPARTMENT = "Department";
 private static final String DESIGNATION = "Designation";
 
 public List getEmployees(String employeeFeed) {
  List employees = new ArrayList();
  try {
   XMLInputFactory inputFactory = XMLInputFactory.newInstance();
   InputStream in = new FileInputStream(employeeFeed);
   XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

   Employee employee = null;
   while (eventReader.hasNext()) {
    XMLEvent event = eventReader.nextEvent();
    if (event.isStartElement()) {
     if (event.asStartElement().getName().getLocalPart().equals(EMPLOYEE)) {
      employee = new Employee();
     }
     if (event.asStartElement().getName().getLocalPart().equals(FIRSTNAME)) {
      event = eventReader.nextEvent();
      employee.setFirstName(event.asCharacters().getData());
      continue;
     }
     if (event.asStartElement().getName().getLocalPart().equals(LASTNAME)) {
      event = eventReader.nextEvent();
      employee.setLastName(event.asCharacters().getData());
      continue;
     }
     if (event.asStartElement().getName().getLocalPart().equals(DEPARTMENT)) {
      event = eventReader.nextEvent();
      employee.setDepartment(event.asCharacters().getData());
      continue;
     }
     if (event.asStartElement().getName().getLocalPart().equals(DESIGNATION)) {
      event = eventReader.nextEvent();
      employee.setDesignation(event.asCharacters().getData());
      continue;
     }
    }
    if (event.isEndElement()) {
     if (event.asEndElement().getName().getLocalPart().equals(EMPLOYEE)) {
      employees.add(employee);
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return employees;
 }

 public static void main(String[] args) {
  String employeeFeed = "F:\\jbasics\\examples\\employee.xml";
  CustomXMLEventReader reader = new CustomXMLEventReader();
  List employees = reader.getEmployees(employeeFeed);
  for (Employee employee : employees) {
   System.out.println(employee.getFirstName() + " " + employee.getLastName());
   System.out.println("Department : "+ employee.getDepartment());
   System.out.println("Designation : " + employee.getDesignation());
   System.out.println("---------------------------------------");
  }
 }
}

2 comments:

  1. Its looks preety cool......

    One query:

    How about adding more nodes into .xml file?

    is I need to change my code for that?

    ReplyDelete
  2. Any parser works against an XSD so if the data definition changes, parser will need to operate on the new data-def and hence the code needs to be changed.

    ReplyDelete