Sunday, 11 October 2015

XML PARSING IN PYTHON USING MINIDOM

parsing an xml in python using minidom

xml file to be parsed

<?xml version="1.0"?>
<company>
    <employee id="1001">
        <firstname>yong</firstname>
        <lastname>mna</lastname>
        <nickname>mrreferenc</nickname>
        <salary>100000</salary>
    </employee>
    <employee id="2001">
        <firstname>dev</firstname>
        <lastname>journal</lastname>
        <nickname>general</nickname>
        <salary>200000</salary>
    </employee>
       <employee id="2002">
        <firstname>darth</firstname>
        <lastname>vader</lastname>
        <nickname>anakan</nickname>
        <salary>2000</salary>
    </employee>
<employee id="2003">
        <firstname>obi</firstname>
        <lastname>van kanobi</lastname>
        <nickname>ben kanobi</nickname>
        <salary>1500</salary>
    </employee>
<employee id="2009">
        <firstname>hello</firstname>
        <lastname>world</lastname>
        <nickname>wasteland</nickname>
        <salary>20</salary>
    </employee>
</company>

CODE:
   
__author__ = 'solomon'
from xml.dom import minidom

xmlfile= minidom.parse('/home/solomon/solo.xml')
employee = xmlfile.getElementsByTagName("employee")
for emp in employee:
    if(emp.hasAttribute('id')):
        print emp.getAttribute('id')
    firstname= emp.getElementsByTagName('firstname')[0]
    print "firtname" + firstname.childNodes[0].data
    lastname=emp.getElementsByTagName('lastname')[0]
    print 'lastname'+lastname.childNodes[0].data
    salry=emp.getElementsByTagName('salary')[0]
    print "salary"+salry.childNodes[0].data
 
 
OUTPUT:
 
/usr/bin/python2.7 /home/solomon/PycharmProjects/untitled/domparsing.py
1001
firtnameyong
lastnamemna
salary100000
2001
firtnamedev
lastnamejournal
salary200000
2002
firtnamedarth
lastnamevader
salary2000
2003
firtnameobi
lastnamevan kanobi
salary1500
2009
firtnamehello
lastnameworld
salary20

Process finished with exit code 0
 

Sunday, 4 October 2015

OBJECT SORTING IN JAVA COMPARATOR VS COMPARABLE

comparable and comparators are two interfaces provided by java to Sort custom Objects . Here i'm going to explain to you how to sort a person class using java


package sample;
import  com.solo.Adress;public class Person implements Comparable<Person> {

    private String name;    private String age;    private Adress adress;
    public String getName() {
        return name;    }

    public void setName(String name) {
        this.name = name;    }


    public String getAge() {
        return age;    }

    public Adress getAdress() {
        return adress;    }

    public void setAdress(Adress adress) {
        this.adress = adress;    }

    public void setAge(String age) {
        this.age = age;    }


    public Person(String name, String age, Adress adress) {
        this.name = name;        this.age = age;        this.adress = adress;    }


    @Override

    public int compareTo(Person person) {
        return this.name.compareTo(person.getName());    }
}

THis is a simple example for comparable interface it is self explanatory .it has one method

very simple and used for normal sorting operations 


what if you want more than two sorting techniques i.e you want to sort by both age and salry 

that's where comparator kicks in
 
package sample;
import java.util.Comparator;
public class Agecomparator implements Comparator<Person> {
    @Override    public int compare(Person person, Person t1) {
        return person.getAge().compareTo(t1.getAge());    }
}

System.out.println("before Sorting");printperson(personList);
System.out.println("....................sorting........................");
Collections.sort(personList,new  Agecomparator());
System.out.println("....................... After sorting --------------------------");
printperson(personList);


before Sorting
solomon
anish
bob
zolton
farage
hercules
....................sorting........................
....................... After sorting --------------------------
farage
hercules
zolton
anish
solomon
bob