Ở ví dụ này chúng ta chỉ validate bằng DTD không validate bằng XSD.
Chạy hàm main bằng Argument args[] truyền 1 file xml
DTD validate những cái gì ?
DTD kiểm tra các thẻ element có viết đúng hay không.
Cách khai báo internal or external DTD và XSD trong file XML [Xem]
Tìm hiểu về DTD và XSD [Xem]
sinhvien.xml
Java XML 2016
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE people SYSTEM "sinhvien.dtd">
<people>
<person ID="001">
<title>MR</title>
<name>Ewin Antonio</name>
<name>Malik Shabazz</name>
<name>Malcolm Little</name>
<born>13 May 1955</born>
<died>20 February 1915</died>
<nationality>american</nationality>
<email>antonio@giaima.com</email>
</person>
<person ID="002">
<title>MR</title>
<name>Mahatma Andraw</name>
<born>20 October 1863</born>
<died>03 January 1958</died>
<nationality>Indian</nationality>
<email>andraw@giaima.com</email>
</person>
<person ID="003">
<title>MR</title>
<name>John F. Benito</name>
<name>JFK</name>
<name>Jack Kennedy</name>
<born>29 January 1901</born>
<died>01 November 1913</died>
<nationality>american</nationality>
<email>benito@giaima.com</email>
</person>
</people>
sinhvien.dtd
Java XML 2016
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT people (person+)>
<!ELEMENT person (title, name+, born, died?, nationality*, email*)>
<!ATTLIST person ID CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT born (#PCDATA)>
<!ELEMENT died (#PCDATA)>
<!ELEMENT nationality (#PCDATA)>
<!ELEMENT email (#PCDATA)>
ValidationDOM.java
Java XML 2016
package validationdtd;
import java.io.IOException;
import java.util.logging.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
/**
*
* @author Lonely
*/
public class ValidationDOM {
public void validDOM(String xmlFile) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new MyErrorHandler());
builder.parse(new InputSource("src\\database\\" + xmlFile));
} catch (ParserConfigurationException ex) {
Logger.getLogger(ValidationDOM.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(ValidationDOM.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ValidationDOM.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
ValidationSAX.java
Java XML 2016
package validationdtd;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.*;
import org.xml.sax.*;
/**
*
* @author Lonely
*/
public class ValidationSAX {
public void validSAX(String xmlFile) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new MyErrorHandler());
reader.parse(new InputSource("src\\database\\"+xmlFile));
} catch (ParserConfigurationException ex) {
Logger.getLogger(ValidationSAX.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(ValidationSAX.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ValidationSAX.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
MyErrorHandler.java
Java XML 2016
package validationdtd;
import org.xml.sax.*;
public class MyErrorHandler implements ErrorHandler {
@Override
public void warning(SAXParseException e) throws SAXException {
System.out.print("Warning at line " + e.getLineNumber() + ": ");
System.out.println(e.getMessage());
}
@Override
public void error(SAXParseException e) throws SAXException {
System.out.print("Error at line " + e.getLineNumber() + ": ");
System.out.println(e.getMessage());
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
System.out.print("Fatal error at line " + e.getLineNumber() + ": ");
System.out.println(e.getMessage());
}
}
Main.java
Java XML 2016
package validationdtd;
/**
*
* @author Lonely
*/
public class Main {
public static void main(String[] args) {
ValidationDOM v = new ValidationDOM();
v.validDOM(args[0]);
ValidationSAX v1 = new ValidationSAX();
v1.validSAX(args[0]);
}
}
B3: Nhập tên file sinhvien.xml vào ô Arguments cách nhau bằng khoảng trắng rồi nhấn OK
B4: Tiến hành chạy project nhấn F6 để chạy toàn bộ project
-Nếu màn hình successful thì file XML không có lỗi
-Ngược lại lỗi sẽ hiển thị
VD: XML lỗi
-Ngược lại lỗi sẽ hiển thị
VD: XML lỗi
XML đúng
XML lỗi
Khi chạy project báo lỗi như sau:
...Còn nhiều lỗi khác.
Download (Zip import NetBeans)
0 nhận xét:
Post a Comment