Chủ Nhật, 10 tháng 8, 2014

Use validator to validate xml against a schema

Demo: Create a program that accepts two command line parameters. The first parameter must specify the name of an XML document and the second parameter must specify the name of a schema. The program should validate the XML document against the specified schema by creating a DOM source on the
document.
Tạo Java Application và file XML shiporder

Tạo file xsd shiporder

Code main class

Kết quả

Demo tại đây

Demo :Create a program that accepts two command line parameters. The first parameter must specify the name of an XML document and the second  parameter must specify the name of Schema. The program must also validate the XML document against the specified schema by creating a SAX source on the 
document.


Tạo new Java Application và file xml

File xsd
Code DefaultHandler trong main class

Code chạy chương trình

Kết quả
Demo tại đây

Nhận xét :
- Validator là một cách để kiểm soát lỗi của một file xml.
- Chương trình được an toàn hơn vì nó không chỉ check xem một file xml có theo quy tắc chuẩn hay không, nó còn kiểm tra một file xml có thỏa mãn một schema nào đó hay không.

Use Xpath

Demo: In an organization, five hundred employees are working. Out of which some are apprentices. The Delivery Unit Head of that organization gave you the responsibility to develop a software, with which you can find the details of apprentices. Use the below files to develop the application:
1. The EmployeeDatabase.xml file holds the data of all employees.
2. Use XPath expressions.
In this application, the apprentices are defined in the trainee namespace while all other employees are defined in the default namespace. This application should count the number of apprentices and print their details on the user console.

Tạo Java Application và tạo file xml

Code tạo kết nối tới file XML vừa tạo trong main class

Code Namespace

Code Xpath

Kết quả
Demo tại đây

Nhận xét :
-Xpath đóng vai trò quan trọng trong quá trình trao đổi thông tin,cơ sở dữ liệu.
-Dễ dàng truy cập vào các node trong XML bằng cách viết các câu lệnh đơn giản và đặt điều kiện trong các câu lệnh.
-Cú pháp khai báo đơn giản và ngắn gọn.
-Cho phép ta chọn lựa những thông tin, dữ liệu nào mà ta muốn chuyển.

Use Trax to tranform xml document

Demo : Create an application, which accepts an XSLT file as its first argument and XML document as second argument. Then, create an application using the following classes, which transforms a Source and sends the output to a Result object. 
1. Use the TransformerFactory class and its methods. 
2. Use the Transformer class and its methods.
Tạo Java Application và tạo file XML


tạo file style

Code sắp xếp tên

Code lấy dữ liệu

Code main class
Thu được file output sau khi chạy chương trình

Chạy file output.html
Demo tại đây

Nhận xét :
-Việc chuyển đổi XML sang các định dạng khác là một nhu cầu thiết yếu.
-Việc chuyển đổi không hề khó khăn và phức tạp.

Apply XQuery

Demo : Read all username in file tomcat-users.xml:

Download các thư viện cần dùng tại đây:



Tạo Java Application và Add các thư viện đã download như hình trên

Tạo file XML

Code main class

Kết quả như sau 

Demo tại đây

Nhận xét :
-XQuery có thể chuyển dữ liệu từ XML sang XHTML dễ dàng.
-Bạn phải viết đúng tên các thẻ và các element.
-XQuery lấy dữ liệu bằng Web Services nên phải có kiến thức về Web Service.

Describe xml database, it types and mapping xml schema and database schema

I.Định nghĩa :
XML đóng vai trò như một database. Có thể truy xuất, cập nhật , quản lí thông tin một cách dễ dàng.Với XML, lưu trữ dữ liệu không khó khăn và nó rất phổ biến.
II.Phân loại:
-XML-Enabled : giúp sử dụng XML
-Native XML : lưu trữ dữ liệu dưới dạng XML
III.Tác dụng :
-Dùng để mô tả thông tin dữ liệu.

IV.Tổ chức cơ sở dữ liệu: 
A.Gồm có 2 loại
-Data-Centric Document: tổ chức dữ liệu theo cấu trúc.
-Document-Centric Documents: XML không có cấu trúc chặt chẽ được như Data-Centric Documents
-Dữ liệu trong file XML được lưu dưới dạng node
-Node có thể biến thành Object và tồn tại thư viện có thể biến object thành XML và ngược lại 
B.Native XML Databases
-Các thông tin được lưu trữ dạng XML
-Hỗ trợ truy xuất dữ liệu = query
-Giúp đỡ cập nhật dữ liệu
-Mapping giữa  CSDL dạng quan hệ và CSDL XML
-Mapping và truy xuất đơn giản
Mapping giữa CSDL XML và object phức tạp hơn CSDL dạng quan hệ và CSDL XML :
Nhận xét :
-Việc quản lí cơ sở dữ liệu với XML không khó khăn.
-XML không phụ thuộc vào nền tảng công nghệ.
-Thông tin có thể trùng nhau.

Use TreeWalker to read xml file

Create an XML document containing the employee details of an organization. Then create an application, which uses the DOM parser to parse the XML document into a DOM tree and traverses through it. The application should implement the TreeWalker interface to traverse all the nodes present in the DOM tree and print the value of the nodes.
Tạo Java Application với file xml như sau

Code main class

Kết quả 
Demo tại đây

 Nhận xét :
-Thể hiện tốt cấu trúc tài liệu thông qua các node.
-TreeWalker duy trì các mối quan hệ thứ bậc của nhánh con, cho phép chuyển hướng của hệ thống phân cấp này.

Use NodeIterator to read xml file

Create an application demonstrating traversal of a DOM tree using theTreeWalker interface. Create an XML document containing the employee details of an organization. Then create an application, which uses the DOM parser to parse the XML document into a DOM tree and traverses through it. The application should implement the TreeWalker interface to traverse through all the nodes present in the DOM tree and print the value of the nodes.
Tạo Java Application và file XML như trên.

Code main class

Kết Quả
Demo tại đây


Nhận xét :
-Làm việc với file XML dễ dàng và an toàn hơn.
-Dễ sử dụng và rất mạnh mẽ