Thứ Hai, 28 tháng 7, 2014

Use SAX to read XML file

Demo 1:
Create a program to accept the tomcat-users.xml file as a command line parameter and count the number of users defined in this configuration file. At the end of processing, it requires to print the number of users on the console.
Bài làm:
Bước 1: Tạo Project chúng ta chọn Java Application.
Bước 2: Tạo tomcat-user.xml

Bước 3: Tạo TagCouter.java

Bước 4: Viết code cho file chạy ứng dụng.

Demo chương trình.
Các bạn có thể tải Demo tại đây.

Demo 2:
Create the student.xml file that stores a list of registered students. Write a program for printing the name and ID of all registered students provided in student.xml. Make necessary arrangements to accept a command line parameter from the user to specify the name of the XML document that holds the list of registered students.
Bài làm:
Bước 1: Chúng ta cũng tạo Project giống như Demo 1.
Bước 2: Tạo file student.xml

Bước 3: Tạo file Student.java

Bước 4: Viết code cho file chạy ứng dụng.
Demo chương trình.
Các bạn có thể tải Demo tại đây.

Demo 3:
Write a program which demonstrates the use of SAX parser for a simple search in XML document. The program searches a specified user in the tomcat-users.xml file and prints an the role of the given user on search completion. This program should include two command-line parameters. The first parameter specifies the tomcat-users.xml file and the second parameter specifies the user name to search in the configuration file.
Bài làm:
Bước 1: Chúng ta tạo Project giống như Demo 1 và Demo 2.
Bước 2: Tạo file user.xml

Bước 3: Tạo file User.java

Bước 4: Xử lí Code trong file chạy chương trình.

Demo chương trình.
Các bạn có thể tải Demo tại đây.

Qua các bài Demo ta có thể nhận thấy dùng SAX khá đơn giản và dùng ít bộ nhớ nên tốc độ xử lý nhanh,khá dễ sử dụng để lọc dữ liệu nhưng qua đó ta cũng có thể thấy một số nhược điểm của nó như là không thể tác động lên dữ liệu XML và chế độ lưu trữ trạng thái phức tạp.Một điều nữa là sẽ khó khăn khi phân tích cấu trúc XML lớn.Nên ta chỉ dùng cho các ứng dụng đơn giản.


Không có nhận xét nào:

Đăng nhận xét