Java XML Validate validateXML(String xml, String schemaURI, DefaultHandler handler)

Here you can find the source of validateXML(String xml, String schemaURI, DefaultHandler handler)

Description

This method will validate the the XML String passed in afainst the Schema Location passed in as a parameter.

License

Open Source License

Parameter

Parameter Description
xml a parameter
schemaURI a parameter
handler a parameter

Exception

Parameter Description
IOException an exception
SAXException an exception
ParserConfigurationException an exception

Declaration

public static void validateXML(String xml, String schemaURI, DefaultHandler handler)
        throws IOException, SAXException, ParserConfigurationException 

Method Source Code

//package com.java2s;
/** Exhibit A - UIRF Open-source Based Public Software License.
* 
* The contents of this file are subject to the UIRF Open-source Based
* Public Software License(the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* openelis.uhl.uiowa.edu/* w ww.j  av a 2 s . c om*/
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
* 
* The Original Code is OpenELIS code.
* 
* The Initial Developer of the Original Code is The University of Iowa.
* Portions created by The University of Iowa are Copyright 2006-2008. All
* Rights Reserved.
* 
* Contributor(s): ______________________________________.
* 
* Alternatively, the contents of this file marked
* "Separately-Licensed" may be used under the terms of a UIRF Software
* license ("UIRF Software License"), in which case the provisions of a
* UIRF Software License are applicable instead of those above. 
*/

import java.io.ByteArrayInputStream;

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.SAXParserFactory;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.w3c.dom.Document;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Main {
    /**
     * This method will validate the the XML String passed in afainst the Schema
     * Location passed in as a parameter. A handler must be passed in to handle
     * any validation errors. This method requires jdk1.5 in order to work.
     * 
     * @param xml
     * @param schemaURI
     * @param handler
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    public static void validateXML(String xml, String schemaURI, DefaultHandler handler)
            throws IOException, SAXException, ParserConfigurationException {

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.XML_NS_URI);
        StreamSource ss = new StreamSource(schemaURI);
        Schema schema = sf.newSchema(ss);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setSchema(schema);
        spf.setNamespaceAware(true);
        javax.xml.parsers.SAXParser saxParser = spf.newSAXParser();
        saxParser.parse(new ByteArrayInputStream(xml.getBytes()), handler);
    }

    /**
     * This method will return a new Document object parsed from the XML String
     * passed in as a parameter.
     * 
     * @param xml
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     * @throws Exception
     */
    public static Document parse(String xml)
            throws ParserConfigurationException, SAXException, IOException, Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(xml.getBytes()));
    }
}

Related

  1. validateXml(final Schema schema, final byte[] xmlBytes)
  2. validateXml(InputStream xml, InputStream xsd)
  3. validateXML(String schemaPath, String xmlPath)
  4. validateXMLFile(String filename, String schemaFile)
  5. validateXmlOnSchema(File xmlSchema, Source sourceXml)