使用JAVAAPI验证XML是否符合DTD的规定

 本文介绍了使用Java API来验证XML文件是否符合DTD文件的规定。

具体示例如下:

首先建立DTD文件

文件名:person.dtd
文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<!ELEMENT person (name, sex, birthday)*>
<!ELEMENT name (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ELEMENT birthday (#PCDATA)>

然后建立两个利用这个dtd文件的xml文件
1,文件名:person.xml
文件内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE person SYSTEM "person.dtd">
<person>
    <name>tom</name>
    <sex>male</sex>
    <birthday>1949-10-01</birthday>
</person>

2,文件名:worker.xml
文件内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE person SYSTEM "person.dtd">
<person>
    <name>tom</name>
    <sex>male</sex>
    <birthday>1949-10-01</birthday>
    <job>it</job>
</person>

我们可以看到worker.xml文件不符合dtd的规定,多了一个job的标签。

然后建立java文件
文件名:ValidateXMLDTD.java
文件内容:
package xmlvalidate;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class ValidateXMLDTD {
    
    public static void main(String[] args) {
//        System.out.println("测试符合DTD规范的XML文件");
//        testPerson();
        
//        System.out.println("测试不符合DTD规范的XML文件");
//        testWorkder();
        
    }
    
    public static void testPerson() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.parse(new java.io.FileInputStream("person.xml"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void testWorkder() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.parse(new java.io.FileInputStream("worker.xml"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


修改一下main方法中的注释语句,运行一下,执行结果:
运行testPerson的时候,只输出:
测试符合DTD规范的XML文件

而运行testWorker的时候,输入如下内容:
测试不符合DTD规范的XML文件
Warning: validation was turned on but an org.xml.sax.ErrorHandler was not
set, which is probably not what is desired.  Parser will use a default
ErrorHandler to print the first 10 errors.  Please call
the 'setErrorHandler' method to fix this.
Error: URI=null Line=7: Element type "job" must be declared.
Error: URI=null Line=8: The content of element type "person" must match "(name,sex,birthday)*".


你可能感兴趣的:(java,xml,validation,测试,System,encoding)