Sei sulla pagina 1di 6

1.

DomParserExample

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParserExample {

ArrayList<Employee> myEmpls;

Document dom;

public DomParserExample(){
myEmpls = new ArrayList<Employee>();
}

public void runExample() {

parseXmlFile();
parseDocument();
printData();
}

private void parseXmlFile(){


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("packages.xml");

}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}

private void parseDocument(){

Element docEle = dom.getDocumentElement();

NodeList nl = docEle.getElementsByTagName("package");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {

Element el = (Element)nl.item(i);

Employee e = getEmployee(el);

myEmpls.add(e);
}
}
}

private Employee getEmployee(Element empEl) {

int id = Integer.parseInt(empEl.getAttribute("id"));

String name = getTextValue(empEl,"name");


String city = getTextValue(empEl,"city");
String type = getTextValue(empEl,"type");
String price = getTextValue(empEl,"price");

Employee e = new Employee(name,id,city,type,price);

return e;
}

private int getIntValue(Element ele, String tagName) {


return Integer.parseInt(getTextValue(ele,tagName));
}

private String getTextValue(Element ele, String tagName) {


String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);

textVal =nl.item(0).getFirstChild().getNodeValue();

return textVal;
}

private void printData(){


System.out.println("\nNo of Loans : " + myEmpls.size());
Iterator<Employee> it = myEmpls.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}

public static void main(String[] args){


DomParserExample dpe = new DomParserExample();

dpe.runExample();
}
}

2. Employee.java

public class Employee {

private String name;


private int id;
private String city;
private String type;
private String price;

public Employee(){

public Employee(String name, int id, String city, String type, String price) {
this.name = name;
this.id = id;
this.city= city;
this.type = type;
this.price = price;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getCity() {


return city;
}

public void setCity(String city) {


this.city =city;
}

public String getType() {


return type;
}
public void setType(String type) {
this.type = type;
}

public String getPrice(){


return price;
}
public void setPrice(String price){
this.price = price;
}

public String toString() {


StringBuffer sb = new StringBuffer();
sb.append("\nLoan Details {");
sb.append("\n\tId : " + getId());
sb.append(", ");
sb.append("\n\tName : " + getName());
sb.append(", ");
sb.append("\n\tCity : " + getCity());
sb.append(",");
sb.append("\n\tType : " + getType());
sb.append(",");
sb.append("\n\tPrice : " + getPrice());
sb.append(";\n}\n");

return sb.toString();
}
}

3.SAXParserExample

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

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

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserExample extends DefaultHandler {

ArrayList<Employee> myEmpls;

private String tempVal;

private Employee tempEmp;


public SAXParserExample() {
myEmpls = new ArrayList<Employee>();
}

public void runExample() {


parseDocument();
printData();
}

private void parseDocument() {

SAXParserFactory spf = SAXParserFactory.newInstance();


try {

SAXParser sp = spf.newSAXParser();

sp.parse("packages.xml", this);

} catch (SAXException se) {


se.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}

private void printData() {

System.out.println("\nNo of Loans : " + myEmpls.size());

Iterator<Employee> it = myEmpls.iterator();
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}

public void startElement(String uri, String localName, String qName, Attributes


attributes) throws SAXException {
tempVal = "";
if (qName.equalsIgnoreCase("package")) {
tempEmp = new Employee();
tempEmp.setId(Integer.parseInt(attributes.getValue("id")));
}
}

public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch, start, length);
}

public void endElement(String uri, String localName, String qName) throws


SAXException {

if (qName.equalsIgnoreCase("package")) {
myEmpls.add(tempEmp);
} else if (qName.equalsIgnoreCase("name")) {
tempEmp.setName(tempVal);
} else if (qName.equalsIgnoreCase("city")) {
tempEmp.setCity(tempVal);
} else if (qName.equalsIgnoreCase("type")) {
tempEmp.setType(tempVal);
}else if(qName.equalsIgnoreCase("price")){
tempEmp.setPrice(tempVal);
}

public static void main(String[] args) {


SAXParserExample spe = new SAXParserExample();
spe.runExample();
}