Let's see how much time does PHP programmer spend to parse some XML file.
Here is our test XML file:
and this is how I would parse this document in PHP:
and the result would be:
Nice, don't you think?
And this is how I would parse the same file in Java:
Main.java:
package me.tcom.kdarko.blog;Employee.java:
import java.io.IOException;
import java.util.ArrayList;
import org.xml.sax.SAXException;
public class Main
{
private ArrayListarraylist = null ;
public static void main(String[] args)
{
try {
Main m = new Main();
m.arraylist = new EmployeesListParser("list_of_employees.xml").doParse();
for(Employee e:m.arraylist)
System.out.println("Employee: ID: " + e.getId() + " , " + e.getName() + " " + e.getLast_name());
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
catch (SAXException ex)
{
System.out.println(ex.getMessage());
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
package me.tcom.kdarko.blog;EmployeesListParser.java:
public class Employee
{
String id , name, last_name;
public Employee() {
}
public Employee(String id, String name, String last_name) {
this.id = id;
this.name = name;
this.last_name = last_name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package me.tcom.kdarko.blog;and the result would be:
import java.io.IOException;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.sun.org.apache.xerces.internal.parsers.SAXParser;
public class EmployeesListParser implements ContentHandler
{
private String XML_FILE_NAME;
String temp = null;
Employee employee = new Employee();
StringBuffer sb=new StringBuffer();
private ArrayListarraylist = new ArrayList ();
public EmployeesListParser(String XML_FILE_NAME) {
this.XML_FILE_NAME = XML_FILE_NAME;
}
public ArrayListdoParse() throws IOException, SAXException
{
XMLReader parser = new SAXParser();
parser.setContentHandler(this);
parser.parse(XML_FILE_NAME);
return arraylist;
}
public void startElement(String uri, String name, String qName, Attributes atts) throws SAXException
{
sb.setLength(0);
if(name.equalsIgnoreCase("id"))
temp="id";
else if(name.equalsIgnoreCase("name"))
temp="name";
else if(name.equalsIgnoreCase("last_name"))
temp = "last_name";
else if(name.equalsIgnoreCase("employee"))
employee = new Employee();
}
public void endElement(String uri, String name, String qName) throws SAXException
{
sb.setLength(0);
if(name.equalsIgnoreCase("employee"))
{
arraylist.add(employee);
employee=null;
}
}
public void characters(char[] arg0, int arg1, int arg2) throws SAXException
{
sb.append(arg0,arg1,arg2);
String s = sb.toString().trim();
if(!s.equals(""))
{
if( temp.equalsIgnoreCase("id") )
{
employee.setId( s );
}
else if( temp.equalsIgnoreCase("name") )
{
employee.setName( s );
}
else if( temp.equalsIgnoreCase("last_name") )
{
employee.setLast_name(s);
}
}
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
}
public void endDocument() throws SAXException {
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
public void endPrefixMapping(String prefix) throws SAXException {
}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
public void processingInstruction(String target, String data) throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
}
:-)
No comment...
No comments:
Post a Comment