博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml 校验
阅读量:4479 次
发布时间:2019-06-08

本文共 8624 字,大约阅读时间需要 28 分钟。

package sax.parsing;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import javax.xml.XMLConstants;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import javax.xml.validation.Schema;import javax.xml.validation.SchemaFactory;import org.dom4j.DocumentException;import org.dom4j.io.SAXReader;import org.dom4j.io.SAXValidator;import org.testng.annotations.Test;import org.w3c.dom.Document;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLReaderFactory;public class ErrorProcessor extends DefaultHandler {    @Override    public void warning(SAXParseException exception) throws SAXException {        System.out.println("触发警告:");        System.err.println("warning: " + getLocationString(exception) + ": " + exception.getMessage());    }    @Override    public void error(SAXParseException exception) throws SAXException {        System.out.println("触发错误:");        System.err.println("error: " + getLocationString(exception) + ": " + exception.getMessage());    }    @Override    public void fatalError(SAXParseException exception) throws SAXException {        System.out.println("触发致命错误:");        System.err.println("fatal error: " + getLocationString(exception) + ": " + exception.getMessage());    }    private String getLocationString(SAXParseException ex) {        StringBuffer buffer = new StringBuffer();        String publicId = ex.getPublicId();        if (publicId != null) {            buffer.append(publicId);            buffer.append("  ");        }        String systemId = ex.getSystemId();        if (systemId != null) {            buffer.append(systemId);            buffer.append(':');        }        buffer.append(ex.getLineNumber());        buffer.append(':');        buffer.append(ex.getColumnNumber());        return buffer.toString();    }    @Override    public void endElement(String uri, String localName, String qName) throws SAXException {        System.out.println("
"); } /** * 在DOM文件构建工厂中设置校验Schema文件 * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ @Test public void parseWithSchema() throws SAXException, IOException, ParserConfigurationException { System.out.println("========== parseWithSchema() start ==============="); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new File("src/students.xsd")); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setSchema(schema); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setErrorHandler(new ErrorProcessor()); Document doc = builder.parse("src/students.xml"); System.out.println("========== parseWithSchema() end ==============="); } @Test public void read() throws FileNotFoundException, IOException, SAXException { System.out.println("========== read() start (仅语法校验) ==============="); XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/validation", true); xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); xmlReader.setErrorHandler(new ErrorProcessor()); xmlReader.parse(new InputSource(new FileInputStream("src/students.xml"))); System.out.println("========== read() end ==============="); } @Test public void saxValidate() throws ParserConfigurationException, SAXException, DocumentException, FileNotFoundException, IOException { System.out.println("========== saxValidate() start ==============="); SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(true); // 等价于 xmlReader.setFeature("http://xml.org/sax/features/validation", true); parserFactory.setNamespaceAware(true); // 等价于 reader.setFeature("http://xml.org/sax/features/namespaces",true); SAXParser parser = parserFactory.newSAXParser(); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:/D:/eclipse-luna-jee/workspace/xsl_trans/src/students.xsd"); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setErrorHandler(new ErrorProcessor()); // 错误时触发 //xmlReader.setContentHandler(new ErrorProcessor()); // 标签开始、结束等事件时触发 xmlReader.parse(new InputSource(new FileInputStream("src/students.xml"))); System.out.println("========== saxValidate() end ==============="); } @Test public void dom4jValidate() throws ParserConfigurationException, SAXException, DocumentException, FileNotFoundException, IOException { System.out.println("========== dom4jValidate() start ==============="); SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(true); // 等价于 xmlReader.setFeature("http://xml.org/sax/features/validation", true); parserFactory.setNamespaceAware(true); // 等价于 reader.setFeature("http://xml.org/sax/features/namespaces",true); SAXParser parser = parserFactory.newSAXParser(); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:/D:/eclipse-luna-jee/workspace/xsl_trans/src/students.xsd"); XMLReader xmlReader = parser.getXMLReader(); /* * dom4j的校验处理过程 * org.dom4j.io.SAXReader * org.dom4j.Document * org.dom4j.io.SAXValidator */ SAXReader reader = new SAXReader(); org.dom4j.Document doc = reader.read(new File("src/students.xml")); SAXValidator validator = new SAXValidator(xmlReader); validator.setErrorHandler(new ErrorProcessor()); validator.validate(doc); System.out.println("========== dom4jValidate() end ==============="); }} 输出:

========== parseWithSchema() start ===============

error: file:///D:/eclipse-luna-jee/workspace/xsl_trans/src/students.xml:4:41: cvc-complex-type.3.2.2: Attribute 'attr_test' is not allowed to appear in element 'student'.触发错误:

 

触发错误:

error: file:///D:/eclipse-luna-jee/workspace/xsl_trans/src/students.xml:10:40: cvc-complex-type.2.4.a: Invalid content was found starting with element 'elem_test1'. One of '{student}' is expected.
========== parseWithSchema() end ===============

 

========== read() start (仅语法校验) ===============
触发错误:
error: 3:10: Document is invalid: no grammar found.
触发错误:
error: 3:10: Document root element "students", must match DOCTYPE root "null".
========== read() end ===============

 

========== saxValidate() start ===============
触发错误:
error: 4:41: cvc-complex-type.3.2.2: Attribute 'attr_test' is not allowed to appear in element 'student'.
触发错误:
error: 10:40: cvc-complex-type.2.4.a: Invalid content was found starting with element 'elem_test1'. One of '{student}' is expected.
========== saxValidate() end ===============

 

 

========== dom4jValidate() start ===============

触发错误:
error: file:///D:/eclipse-luna-jee/workspace/xsl_trans/src/students.xml:3:41: cvc-complex-type.3.2.2: Attribute 'attr_test' is not allowed to appear in element 'student'.
触发错误:
error: file:///D:/eclipse-luna-jee/workspace/xsl_trans/src/students.xml:9:39: cvc-complex-type.2.4.a: Invalid content was found starting with element 'elem_test1'. One of '{student}' is expected.
========== dom4jValidate() end ===============

 

 

students.xml

张三
18
100
lisi
20
100

 

students.xsd

 

转载于:https://www.cnblogs.com/asnjudy/p/4236099.html

你可能感兴趣的文章
MySQL的explain中的参数说明
查看>>
JAVA基本数据类型、引用数据类型-参数传递详解
查看>>
sun.misc.Unsafe 详解
查看>>
食堂排队问题的一个实现
查看>>
Git 回滚代码的正确姿势
查看>>
构造函数、析构函数、虚析构函数、纯虚析构函数要点
查看>>
顺序栈用C语言实现
查看>>
Python批量获取京东商品列表信息
查看>>
2017.7.10 C组总结
查看>>
SourceTree下载 及使用
查看>>
MyEclipse下安装FatJar打包工具
查看>>
什么是域名-视频讲解?
查看>>
大道至简第六章-从编程到工程
查看>>
单元测试——隔离神器:mockito
查看>>
[Web Tools] 实用的Web开发工具
查看>>
ContentProvider
查看>>
欢迎来到Attention的博客
查看>>
获取IOS bundle中的文件
查看>>
document
查看>>
定义DoubleArray并将其作为value写入SequenceFile
查看>>