Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, 16 September 2008

Parsing XML: PHP vs Java

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;

import java.io.IOException;
import java.util.ArrayList;
import org.xml.sax.SAXException;

public class Main
{
   
    private ArrayList arraylist = 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());
        } 
    }

}
Employee.java:

package me.tcom.kdarko.blog;

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;
    }
       
}
EmployeesListParser.java:

package me.tcom.kdarko.blog;

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 ArrayList arraylist = new ArrayList();

    public EmployeesListParser(String XML_FILE_NAME) {
        this.XML_FILE_NAME = XML_FILE_NAME;
    }
   
    public ArrayList doParse() 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 {
    }
   
}
 
and the result would be:



:-)
No comment...

Friday, 18 July 2008

Quick tip 1: Session save path Unwriteable (tested with joomla 1.0.x)

One of the problems people often come up to while installing joomla is Session save path Unwriteable error.

In this quick tip you’ll learn how to solve this problem.

Of course , the first thing you need to do is to contact technical support and a webmaster of your server, and to see if he can ( wants to ) help.

If this doesn’t work, install joomla regardless to this error , and in a folder containing configuration.php create new folder , for example temp_folder.
Set it’s CHMOD so it is allowed to write in it.
From configuration.php copy the value of $mosConfig_absolute_path entry ( without quotes ) , and then add a new line to this file:

session_save_path('');

and between quotes in bracets write copied value + /temp_folder

for example, if in configuration.php says:

$mosConfig_absolute_path = '/home/virtual/site76/fst/var/www/html';

Then, we will add a line:

session_save_path(‘/home/virtual/site76/fst/var/www/html/temp_folder’);

save changes. Now try to login to joomla.
Good luck!