XMLParser using SAX – [Read XML]


Hôm nay mình sẻ hướng dẫn các bạn cách để đọc XML file sử dụng SAX.

SAX parser thì làm việc hoàn toàn khác so với DOM parser. Nó không load XML document vào trong memory và nó củng không tạo ra bất kì đối tượng đại diện nào trong XML document. SAX sử dụng chức năng gọi lại Function .

*NOTE* : SAX Parser is faster and uses less memory than DOM parser.

Những method chúng ta cần chú ý nó được gọi lại khi chúng ta duyệt qua các Element.

startDocument() vs endDocument() : Method này được gọi khi chúng ta bắt đầu và kết thức XML Document

startElement() and endElement() : Method này được gọi khi chúng ta bắt đầu và kết thúc một Element.

characters() : Method này được gọi với Text Content ( Nội dung trong 1 element <node> Nội dung</note> .

Chúng ta có file XML sau :

<?xml version="1.0"?>
<company>
	<staff id="1001">
		<firstname>Kobee</firstname>
		<lastname>Bryan</lastname>
		<address>24/3 TN P.16 GV</address>
		<birthdate>10/10/1991</birthdate>
	</staff>
	<staff id="1002">
		<firstname>Jenny</firstname>
		<lastname>Nguyen</lastname>
		<address>123 QT P.11 GV</address>
		<birthdate>18/05/1992</birthdate>
	</staff>
	<staff id="1003">
		<firstname>Justina</firstname>
		<lastname>Tran</lastname>
		<address>Seul 222/222</address>
		<birthdate>01/01/1990</birthdate>
	</staff>
	<staff id="1004">
		<firstname>Bek</firstname>
		<lastname>Botun</lastname>
		<address>Seso LL GV</address>
		<birthdate>20/10/1994</birthdate>
	</staff>
</company>

Chúng ta có Object class : Staff.java

package thaihoanghai.demo2;

public class Staff {
	private String id;
	private String firstname;
	private String lastName;
	private String address;
	private String birthDate;

	public Staff(){

	}

	public Staff(String id, String firstname, String lastName, String address,
			String birthDate) {
		super();
		this.id = id;
		this.firstname = firstname;
		this.lastName = lastName;
		this.address = address;
		this.birthDate = birthDate;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}

	@Override
	public String toString() {
		return "Staff [id=" + id + ", firstname=" + firstname + ", lastName="
				+ lastName + ", address=" + address + ", birthDate="
				+ birthDate + "]";
	}

}

Chúng ta viết một SAXParser cho việc đọc file XML trên như sau

file MySAXParser.java

package thaihoanghai.demo2;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXParser extends DefaultHandler{
	private Staff staff;
	private String temp;
	private List<Staff> listStaff = new ArrayList<Staff>();

	/**
	 * Every time the parser encounters the beginning of a new element,
	 * it calls this method, which resets the string buffer
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		temp = "";
		if(qName.equalsIgnoreCase("staff")){
			staff = new Staff();
			staff.setId(attributes.getValue("id"));
		}
	}
	/**
	 * When the parser encounters the end of an element, it calls this method
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equalsIgnoreCase("staff")){
			listStaff.add(staff);
		}else if(qName.equalsIgnoreCase("firstname")){
			staff.setFirstname(temp);
		}else if(qName.equalsIgnoreCase("lastname")){
			staff.setLastName(temp);
		}else if(qName.equalsIgnoreCase("address")){
			staff.setAddress(temp);
		}else if(qName.equalsIgnoreCase("birthdate")){
			staff.setBirthDate(temp);
		}
	}
	/**
	 * When the parser encounters plain text (not XML element)
	 * it calls this method accumulates them in a string buffer.
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		temp = new String(ch, start, length);
	}

	private void readList(){
		System.out.println("No of staff in company " + listStaff.size() +"'.");
		Iterator<Staff> it = listStaff.iterator();
		while(it.hasNext()){
			System.out.println(it.next().toString());
		}
	}

	public static void main(String[] args) {
		try{
			SAXParserFactory spFactory = SAXParserFactory.newInstance();
			SAXParser sp = spFactory.newSAXParser();
			// Create an instance of class MySAXParser . it defines all the handler method
			MySAXParser handler = new MySAXParser();

			/**
			 * Read UTF-8 XML File using this code
			 */
//			File file = new File("staff.xml");
//			InputStream inputStream = new FileInputStream(file);
//			Reader reader = new InputStreamReader(inputStream,"UTF-8");
//			InputSource is = new InputSource(reader);

			// We will parser file staff.xml base on handler
			//sp.parse(is, handler); // process UTP-8
			sp.parse("staff.xml",handler); // not process UTF-8

			// print for work testing
			handler.readList();

		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

Kết quả sau khi run :

d1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: