您的位置:首页 > 其它

验证 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息