验证 XML 是否符合 Schema 推荐
2006-03-26 01:55
393 查看
为了验证 XML 文档的合法性,通常需要对文档进行验证,要么符合 DTD,要么符合 Schema。在以前的 JAXP 版本中,验证(Validation)是作为解析器(Parser)的一个功能特征来提供的。
JAXP 的 Validation API 把 XML Parser 和 Validator 解耦合了。这样获得了两个好处:
1、支持更多的 Schema 语言
2、在运行时 XML 与 Schema 更容易耦合在一起
以下是一个小例程,演示了用 Validation API 验证 XML 文件是否符合 Schema:
import java.io.*;
import javax.xml.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class TestMain {
public static void main(String[] args) {
try {
String strLang = XMLConstants.W3C_XML_SCHEMA_NS_URI;
SchemaFactory factory = SchemaFactory.newInstance(strLang);
InputStream isSchema = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal.xsd");
StreamSource ss = new StreamSource(isSchema);
Schema schema = factory.newSchema(ss);
Validator validator = schema.newValidator();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream isXML = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal-schema.xml");
Document document = db.parse(isXML);
DOMSource source = new DOMSource(document);
validator.validate(source);
System.out.println("Result : Valid!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Result : Invalid!");
}
}
}
运行环境:JDK 1.4.2 + Xerces 2.8.0 for Java。
有一个小地方要注意,如果用的是 DOMSource,程序中必须加上:dbf.setNamespaceAware(true);
否则会出错,找不到根节点元素的定义,例如:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'personnel'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at TestMain.main(TestMain.java:32)
当然,如果用的是 StreamSource,就不需要这么费劲了。如下:
……
InputStream isXML = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal-schema.xml");
/*
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(isXML);
DOMSource source = new DOMSource(document);
*/
StreamSource source = new StreamSource(isXML);
validator.validate(source);
……
附源代码下载。是一个 Eclipse 工程。为了减少字节数,把 lib 目录下 Xerces 2.8.0 for Java 的 Jar 包删除掉了。下载后,请自行加上:xml-apis.jar 和 xercesImpl.jar。
Xerces 的下载地址为:http://xerces.apache.org
附件:http://down.51cto.com/data/2347371
JAXP 的 Validation API 把 XML Parser 和 Validator 解耦合了。这样获得了两个好处:
1、支持更多的 Schema 语言
2、在运行时 XML 与 Schema 更容易耦合在一起
以下是一个小例程,演示了用 Validation API 验证 XML 文件是否符合 Schema:
import java.io.*;
import javax.xml.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class TestMain {
public static void main(String[] args) {
try {
String strLang = XMLConstants.W3C_XML_SCHEMA_NS_URI;
SchemaFactory factory = SchemaFactory.newInstance(strLang);
InputStream isSchema = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal.xsd");
StreamSource ss = new StreamSource(isSchema);
Schema schema = factory.newSchema(ss);
Validator validator = schema.newValidator();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream isXML = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal-schema.xml");
Document document = db.parse(isXML);
DOMSource source = new DOMSource(document);
validator.validate(source);
System.out.println("Result : Valid!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Result : Invalid!");
}
}
}
运行环境:JDK 1.4.2 + Xerces 2.8.0 for Java。
有一个小地方要注意,如果用的是 DOMSource,程序中必须加上:dbf.setNamespaceAware(true);
否则会出错,找不到根节点元素的定义,例如:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'personnel'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at TestMain.main(TestMain.java:32)
当然,如果用的是 StreamSource,就不需要这么费劲了。如下:
……
InputStream isXML = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal-schema.xml");
/*
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(isXML);
DOMSource source = new DOMSource(document);
*/
StreamSource source = new StreamSource(isXML);
validator.validate(source);
……
附源代码下载。是一个 Eclipse 工程。为了减少字节数,把 lib 目录下 Xerces 2.8.0 for Java 的 Jar 包删除掉了。下载后,请自行加上:xml-apis.jar 和 xercesImpl.jar。
Xerces 的下载地址为:http://xerces.apache.org
附件:http://down.51cto.com/data/2347371
相关文章推荐
- [转]Java 语言的 XML 验证 API---检查文档是否符合模式
- PHP DOM验证xml文档是否符合DTD验证
- 验证XML 是否符合一个scheml
- 验证xml是否符合指定xsd
- 使用JAVAAPI验证XML是否符合DTD的规定
- Java 语言的 XML 验证 API-检查文档是否符合模式
- dom4j通过dtd或者schema验证xml
- XML之Schema验证
- xml基础、DTD验证、Schema验证(备忘)
- XML验证实践(DTD和Schema)
- java中实现xml schema 验证文件
- 验证表单内容是否符合数据库字段的规则
- Schema验证XML
- 利用jQuery.Validate异步验证用户名是否存在(推荐)
- Java程序员从笨鸟到菜鸟之(二十五)XML之Schema验证
- Schema验证XML
- C#中用schema验证xml的合法性
- 验证身份证号码是否符合规则
- ASP.NET中服务器控件Button调用javascript代码运用正则表达式验证TextBox中输入的是否为正整数或正小数,符合要求则继续运行服务器端代码功能的实现
- 单点登录(十一)-----遇到问题-----cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema na