Java XML Validate validateXmlOnSchema(File xmlSchema, Source sourceXml)

Here you can find the source of validateXmlOnSchema(File xmlSchema, Source sourceXml)

Description

Validates XML on a XMLschema

License

Apache License

Parameter

Parameter Description
xmlSchema a parameter
sourceXml a parameter

Declaration

public static void validateXmlOnSchema(File xmlSchema, Source sourceXml) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import javax.xml.transform.Source;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class Main {
    /**/*from   w w w .j a v  a  2  s .  c o m*/
     * Schema factory
     */
    private static final SchemaFactory W3X_XML_SCHEMA_FACTORY = SchemaFactory
            .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

    /**
     * Validates XML on a XMLschema
     *
     * @param xmlSchema
     * @param sourceXml
     */
    public static void validateXmlOnSchema(File xmlSchema, Source sourceXml) {
        Schema schema = newSchema(xmlSchema);
        try {
            schema.newValidator().validate(sourceXml);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not validate '" + sourceXml.getSystemId() + "' with '" + xmlSchema.getName() + "'!", e);
        }
    }

    /**
     * Validates XML on a XMLschema
     *
     * @param xmlSchema
     * @param sourceXml
     */
    public static void validateXmlOnSchema(Source xmlSchema, Source sourceXml) {
        Schema schema = newSchema(xmlSchema);
        try {
            schema.newValidator().validate(sourceXml);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not validate '" + sourceXml.getSystemId() + "' with '" + xmlSchema.getSystemId() + "'!",
                    e);
        }
    }

    /**
     * Creates a new Schema using the default XML implementation
     *
     * @param xmlSchema
     * @return Schema
     */
    public static Schema newSchema(File xmlSchema) {
        try {
            return W3X_XML_SCHEMA_FACTORY.newSchema(xmlSchema);
        } catch (SAXException e) {
            throw new RuntimeException("Could not create schema for file '" + xmlSchema.getName() + "'!", e);
        }
    }

    /**
     * Creates a new Schema using the default XML implementation
     *
     * @param xmlSchema
     * @return Schema
     */
    public static Schema newSchema(Source xmlSchema) {
        try {
            return W3X_XML_SCHEMA_FACTORY.newSchema(xmlSchema);
        } catch (SAXException e) {
            throw new RuntimeException("Could not create schema for file '" + xmlSchema + "'!", e);
        }
    }
}

Related

  1. validateXml(final Schema schema, final byte[] xmlBytes)
  2. validateXml(InputStream xml, InputStream xsd)
  3. validateXML(String schemaPath, String xmlPath)
  4. validateXML(String xml, String schemaURI, DefaultHandler handler)
  5. validateXMLFile(String filename, String schemaFile)