Hướng dẫn này sẽ chỉ cho bạn cách sử dụng SAX Parser tích hợp trong Java để đọc và phân tích cú pháp tệp XML.
Simple API cho XML (SAX Parser) là gì ?
Simple API cho XML (SAX Parser) là một API đẩy, một observer pattern, hướng sự kiện, truy cập tuần tự vào các phần tử tệp XML một cách tuần tự. Trình phân tích cú pháp SAX này đọc tệp XML từ đầu đến cuối, gọi một phương thức khi nó gặp một phần tử hoặc gọi một phương thức khác khi nó tìm thấy văn bản hoặc thuộc tính cụ thể.
SAX nhanh và hiệu quả, yêu cầu ít bộ nhớ hơn nhiều so với DOM, bởi vì SAX không tạo ra một biểu diễn bên trong (cấu trúc cây) của dữ liệu XML như DOM.
Trình phân tích cú pháp SAX nhanh hơn và sử dụng ít bộ nhớ hơn trình phân tích cú pháp DOM. SAX thích hợp để đọc tuần tự các phần tử XML; DOM thích hợp cho thao tác XML như tạo, sửa đổi hoặc xóa các phần tử XML.
Một số sự kiện SAX phổ biến:
- startDocument()và endDocument() – Phương thức được gọi ở đầu và cuối của tài liệu XML.
- startElement()và endElement()– Phương thức được gọi ở đầu và cuối của một phần tử XML.
- characters()– Phương thức được gọi với nội dung văn bản ở giữa phần đầu và phần cuối của một phần tử XML.
Dưới đây là một tệp XML đơn giản.
<name>Sharecs</name>
Trình phân tích cú pháp SAX đọc tệp XML ở trên và gọi các sự kiện hoặc phương thức sau theo tuần tự:
- startDocument()
- startElement() – <name>
- characters()- Sharecs
- endElement()- </name>
- endDocument()
Đọc hoặc phân tích cú pháp tệp XML (SAX Parser)
Ví dụ này cho bạn thấy cách sử dụng các API trình phân tích cú pháp SAX tích hợp trong Java để đọc hoặc phân tích cú pháp một tệp XML.
Dưới đây là một tệp XML.
<?xml version="1.0" encoding="utf-8"?>
<Company>
<staff id="1001">
<name>sharecs</name>
<role>support</role>
<salary currency="USD">5000</salary>
<!-- for special characters like < &, need CDATA -->
<bio><![CDATA[HTML tag <code>testing</code>]]></bio>
</staff>
<staff id="1002">
<name>yflow</name>
<role>admin</role>
<salary currency="EUR">8000</salary>
<bio><![CDATA[a & b]]></bio>
</staff>
</Company>
P/S :Trong tệp XML, đối với những ký tự đặc biệt như <hoặc &, chúng ta cần phải bọc nó bằng CDATA.
Tạo một lớp để mở rộng , org.xml.sax.helpers.DefaultHandler ghi đè và các phương thức để hiển thị tất cả các phần tử, thuộc tính, nhận xét và văn bản XML.startElementendElementcharacters
package com.sharecs.xml.sax.handler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class PrintAllHandlerSax extends DefaultHandler {
private StringBuilder currentValue = new StringBuilder();
@Override
public void startDocument() {
System.out.println("Start Document");
}
@Override
public void endDocument() {
System.out.println("End Document");
}
@Override
public void startElement(
String uri,
String localName,
String qName,
Attributes attributes) {
// reset the tag value
currentValue.setLength(0);
System.out.printf("Start Element : %s%n", qName);
if (qName.equalsIgnoreCase("staff")) {
// get tag's attribute by name
String id = attributes.getValue("id");
System.out.printf("Staff id : %s%n", id);
}
if (qName.equalsIgnoreCase("salary")) {
// get tag's attribute by index, 0 = first attribute
String currency = attributes.getValue(0);
System.out.printf("Currency :%s%n", currency);
}
}
@Override
public void endElement(String uri,
String localName,
String qName) {
System.out.printf("End Element : %s%n", qName);
if (qName.equalsIgnoreCase("name")) {
System.out.printf("Name : %s%n", currentValue.toString());
}
if (qName.equalsIgnoreCase("role")) {
System.out.printf("Role : %s%n", currentValue.toString());
}
if (qName.equalsIgnoreCase("salary")) {
System.out.printf("Salary : %s%n", currentValue.toString());
}
if (qName.equalsIgnoreCase("bio")) {
System.out.printf("Bio : %s%n", currentValue.toString());
}
}
// http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html#characters%28char%5B%5D,%20int,%20int%29
// SAX parsers may return all contiguous character data in a single chunk,
// or they may split it into several chunks
@Override
public void characters(char ch[], int start, int length) {
// The characters() method can be called multiple times for a single text node.
// Some values may missing if assign to a new string
// avoid doing this
// value = new String(ch, start, length);
// better append it, works for single or multiple calls
currentValue.append(ch, start, length);
}
}
SAX Parser để phân tích cú pháp một tệp XML.
package com.sharecs.xml.sax;
import com.sharecs.xml.sax.handler.PrintAllHandlerSax;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
public class ReadXmlSaxParser {
private static final String FILENAME = "src/main/resources/staff.xml";
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = factory.newSAXParser();
PrintAllHandlerSax handler = new PrintAllHandlerSax();
saxParser.parse(FILENAME, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
}
Kết quả:
Start Document
Start Element : Company
Start Element : staff
Staff id : 1001
Start Element : name
End Element : name
Name : sharecs
Start Element : role
End Element : role
Role : support
Start Element : salary
Currency :USD
End Element : salary
Salary : 5000
Start Element : bio
End Element : bio
Bio : HTML tag <code>testing</code>
End Element : staff
Start Element : staff
Staff id : 1002
Start Element : name
End Element : name
Name : yflow
Start Element : role
End Element : role
Role : admin
Start Element : salary
Currency :EUR
End Element : salary
Salary : 8000
Start Element : bio
End Element : bio
Bio : a & b
End Element : staff
End Element : Company
End Document
Chuyển đổi một fileXML thành một đối tượng
Ví dụ này phân tích cú pháp một tệp XML và chuyển đổi nó thành một List trong các đối tượng. Nó hoạt động, nhưng không được khuyến khích, hãy thử JAXB.
Xem lại cùng một file XML.
<?xml version="1.0" encoding="utf-8"?>
<Company>
<staff id="1001">
<name>sharecs</name>
<role>support</role>
<salary currency="USD">5000</salary>
<!-- for special characters like < &, need CDATA -->
<bio><![CDATA[HTML tag <code>testing</code>]]></bio>
</staff>
<staff id="1002">
<name>yflow</name>
<role>admin</role>
<salary currency="EUR">8000</salary>
<bio><![CDATA[a & b]]></bio>
</staff>
</Company>
Và chúng ta chuyển đổi tệp XML ở trên thành Staff đối tượng sau.
package com.sharecs.xml.sax.model;
import java.math.BigDecimal;
public class Staff {
private Long id;
private String name;
private String role;
private BigDecimal salary;
private String Currency;
private String bio;
//... getters, setters...toString
}
Lớp dưới đây sẽ thực hiện chuyển đổi XML sang Đối tượng ( XML to Object conversion )
package com.sharecs.xml.sax.handler;
import com.sharecs.xml.sax.model.Staff;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class MapStaffObjectHandlerSax extends DefaultHandler {
private StringBuilder currentValue = new StringBuilder();
List<Staff> result;
Staff currentStaff;
public List<Staff> getResult() {
return result;
}
@Override
public void startDocument() {
result = new ArrayList<>();
}
@Override
public void startElement(
String uri,
String localName,
String qName,
Attributes attributes) {
// reset the tag value
currentValue.setLength(0);
// start of loop
if (qName.equalsIgnoreCase("staff")) {
// new staff
currentStaff = new Staff();
// staff id
String id = attributes.getValue("id");
currentStaff.setId(Long.valueOf(id));
}
if (qName.equalsIgnoreCase("salary")) {
// salary currency
String currency = attributes.getValue("currency");
currentStaff.setCurrency(currency);
}
}
public void endElement(String uri,
String localName,
String qName) {
if (qName.equalsIgnoreCase("name")) {
currentStaff.setName(currentValue.toString());
}
if (qName.equalsIgnoreCase("role")) {
currentStaff.setRole(currentValue.toString());
}
if (qName.equalsIgnoreCase("salary")) {
currentStaff.setSalary(new BigDecimal(currentValue.toString()));
}
if (qName.equalsIgnoreCase("bio")) {
currentStaff.setBio(currentValue.toString());
}
// end of loop
if (qName.equalsIgnoreCase("staff")) {
result.add(currentStaff);
}
}
public void characters(char ch[], int start, int length) {
currentValue.append(ch, start, length);
}
}
Chạy nó.
package com.sharecs.xml.sax;
import com.sharecs.xml.sax.handler.MapStaffObjectHandlerSax;
import com.sharecs.xml.sax.model.Staff;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class ReadXmlSaxParser2 {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try (InputStream is = getXMLFileAsStream()) {
SAXParser saxParser = factory.newSAXParser();
// parse XML and map to object, it works, but not recommend, try JAXB
MapStaffObjectHandlerSax handler = new MapStaffObjectHandlerSax();
saxParser.parse(is, handler);
// print all
List<Staff> result = handler.getResult();
result.forEach(System.out::println);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
// get XML file from resources folder.
private static InputStream getXMLFileAsStream() {
return ReadXmlSaxParser2.class.getClassLoader().getResourceAsStream("staff.xml");
}
}
Kết quả:
Staff{id=1001, name='揚木金', role='support', salary=5000, Currency='USD', bio='HTML tag <code>testing</code>'}
Staff{id=1002, name='yflow', role='admin', salary=8000, Currency='EUR', bio='a & b'}
Trình xử lý lỗi SAX
Ví dụ này cho thấy cách đăng ký trình xử lý lỗi tùy chỉnh cho SAX parser.
Tạo một lớp và mở rộng org.xml.sax.ErrorHandler. Đọc mã để tự giải thích. Nó chỉ gói thông báo lỗi ban đầu.
package com.sharecs.xml.sax.handler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.PrintStream;
public class CustomErrorHandlerSax implements ErrorHandler {
private PrintStream out;
public CustomErrorHandlerSax(PrintStream out) {
this.out = out;
}
private String getParseExceptionInfo(SAXParseException spe) {
String systemId = spe.getSystemId();
if (systemId == null) {
systemId = "null";
}
String info = "URI=" + systemId + " Line="
+ spe.getLineNumber() + ": " + spe.getMessage();
return info;
}
public void warning(SAXParseException spe) throws SAXException {
out.println("Warning: " + getParseExceptionInfo(spe));
}
public void error(SAXParseException spe) throws SAXException {
String message = "Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
public void fatalError(SAXParseException spe) throws SAXException {
String message = "Fatal Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
}
Chúng ta sử dụngsaxParser.getXMLReader()để có đượcorg.xml.sax.XMLReader, nó cung cấp nhiều tùy chọn hơn để định cấu hình SAX parser.
package com.sharecs.xml.sax;
import com.sharecs.xml.sax.handler.CustomErrorHandlerSax;
import com.sharecs.xml.sax.handler.MapStaffObjectHandlerSax;
import com.sharecs.xml.sax.model.Staff;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class ReadXmlSaxParser3 {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try (InputStream is = getXMLFileAsStream()) {
SAXParser saxParser = factory.newSAXParser();
// parse XML and map to object, it works, but not recommend, try JAXB
MapStaffObjectHandlerSax handler = new MapStaffObjectHandlerSax();
// try XMLReader
//saxParser.parse(is, handler);
// more options for configuration
XMLReader xmlReader = saxParser.getXMLReader();
// set our custom error handler
xmlReader.setErrorHandler(new CustomErrorHandlerSax(System.err));
xmlReader.setContentHandler(handler);
InputSource source = new InputSource(is);
xmlReader.parse(source);
// print all
List<Staff> result = handler.getResult();
result.forEach(System.out::println);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
// get XML file from resources folder.
private static InputStream getXMLFileAsStream() {
return ReadXmlSaxParser2.class.getClassLoader().getResourceAsStream("staff.xml");
}
}
Cập nhật staff.xml, xóa phần tử CDATA trong bio phần tử và đặt một &, và trình phân tích cú pháp SAX sẽ gặp lỗi.
<?xml version="1.0" encoding="utf-8"?>
<Company>
<staff id="1001">
<name>sharecs</name>
<role>support</role>
<salary currency="USD">5000</salary>
<!-- for special characters like < &, need CDATA -->
<bio>&</bio>
</staff>
</Company>
Chạy nó với trình xử lý lỗi tùy chỉnh ở trên.
xmlReader.setErrorHandler(new CustomErrorHandlerSax(System.err));
Kết quả:
org.xml.sax.SAXException: Fatal Error: URI=null Line=8: The entity name must immediately follow the '&' in the entity reference.
at com.sharecs.xml.sax.handler.CustomErrorHandlerSax.fatalError(CustomErrorHandlerSax.java:41)
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:181)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1471)
//...
Chạy nó mà không có trình xử lý lỗi tùy chỉnh.
// xmlReader.setErrorHandler(new CustomErrorHandlerSax(System.err));
Kết quả:
[Fatal Error] :8:15: The entity name must immediately follow the '&' in the entity reference.
org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 15; The entity name must immediately follow the '&' in the entity reference.
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1243)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
at com.sharecs.xml.sax.ReadXmlSaxParser2.main(ReadXmlSaxParser2.java:44)
SAX và Unicode
Đối với các tệp XML có chứa các ký tự Unicode, theo mặc định, SAX có thể tuân theo mã hóa XML (UTF-8 mặc định) và phân tích cú pháp nội dung một cách chính xác.
Chúng ta có thể xác định mã hóa ở đầu tệp XML , encoding=”encoding-code”; ví dụ: dưới đây là một tệp XML sử dụng UTF-8 mã hóa.
<?xml version="1.0" encoding="utf-8"?>
<Company>
<staff id="1001">
<name>揚木金</name>
<role>support</role>
<salary currency="USD">5000</salary>
<bio><![CDATA[HTML tag <code>testing</code>]]></bio>
</staff>
<staff id="1002">
<name>yflow</name>
<role>admin</role>
<salary currency="EUR">8000</salary>
<bio><![CDATA[a & b]]></bio>
</staff>
</Company>
Ngoài ra, chúng ta có thể xác định một mã hóa được chỉ định trong InputSource.
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(handler);
InputSource source = new InputSource(is);
// set encoding
source.setEncoding(StandardCharsets.UTF_8.toString());
//source.setEncoding(StandardCharsets.UTF_16.toString());
xmlReader.parse(source);
Lưu ý: Thêm các ví dụ về trình phân tích cú pháp SAX – Oracle – API đơn giản cho XML (SAX)
Cảm ơn các bạn đã ghé thăm. Chúc các bạn thành công!