Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533543
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2010-12-04 21:47:30

import java.io.IOException;
import java.io.InputStream;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class Test {
    protected void validateXmlSchema(InputStream xmlFileIn)
            throws SAXException, IOException {
        // reference
        // http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html
        //
        // 1. 构建W3C XML Schema工厂。
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // 2. 编译指定的Schema。

        InputStream xsdIn = Test.class.getResourceAsStream("test.xsd");
        Source xsdSrc = new StreamSource(xsdIn);
        Schema schema = factory.newSchema(xsdSrc);

        // 3. 从schema获取Validator。
        Validator validator = schema.newValidator();

        // 4. 获取要校验的XML文档。
        Source source = new StreamSource(xmlFileIn); // 使用StAX
        // Source source = new SAXSource(new InputSource(xmlFileIn)); // 使用SAX
        // 5. 验证
        validator.validate(source);
    }
}



补充说明:
  如果,要验证的XML没有声明命名空间,则使用以下方式进行:

package me.test;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.helpers.XMLReaderFactory;

public class XmlSchemaValidator {
    public static void main(String[] args) throws SAXException, IOException {

        // 1. 构建W3C XML Schema工厂。
        SchemaFactory factory = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // 2. 编译指定的Schema。
        InputStream xsdIn = XmlSchemaValidator.class
                .getResourceAsStream("exam.xsd");
        Source xsdSrc = new StreamSource(xsdIn);
        Schema schema = factory.newSchema(xsdSrc);

        // 3. 从schema获取Validator。

        Validator validator = schema.newValidator();

        // 4. 获取要校验的XML文档。
        InputStream xslIn = XmlSchemaValidator.class
                .getResourceAsStream("exam.xml");
        // 若果XML数据文件中有正确的namespace
        // Source source = new StreamSource(xslIn);
        // 否则
        Source source = new SAXSource(new NamespaceFilter(XMLReaderFactory
                .createXMLReader()), new InputSource(xslIn));

        // 5. 验证
        validator.validate(source);
        System.out.println("验证通过");
    }

    // 如果 XML是 没有namespace,比如是:...
    // 而不是 ...
    // 则需要此filter。
    protected static class NamespaceFilter extends XMLFilterImpl {
        String requiredNamespace = "";

        public NamespaceFilter(XMLReader parent) {
            super(parent);
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes atts) throws SAXException {
            if (!requiredNamespace.equals(uri)) {
                uri = requiredNamespace;
            }
            super.startElement(uri, localName, qName, atts);
        }
    }
}


阅读(733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~