开发者

How to validate an XML using XSD InputStream using oracle.xml.parser.v2.DOMParser

I am trying to validate an XML file against a XSD document. There are two ways by which I am doing.

  1. Both XML and XSDs are files

  2. XML is a file and XSD is a stream

In the case of XML and XSDs are files,

  validateXML(xmlFile, xsdFile)
{

    m_domParser = new DOMParser();

    String url       = "file:" + new File(xml).getAbsolutePath();
    String xsdUrl       = "file:" + new File(xsd).getAbsolutePath();
         try
         {
             m_domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
             m_domParser.setXMLSchema(xsd);

             Validator handler = new Validator();

             m_domParser.setErrorHandler(handler);
             m_domParser.parse(url);

             m_xmlDoc = m_domParser.getDocument();
             //determine what kinda utility requested

         }

}

It works fine and validating correctly. Here is the code I have written for validating using XSD info as stream

import org.xml.sax.InputSource;
import oracle.xml.parser.v2.DOMParser;

validateXML(String xmlFile, InputStream is)
    Reader reader;
      try {
            m_domParser = new DOMParser();
            m_domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);

            //get the XMLSchema from the input stream
            XSDBuilder builder = null;
            XMLSchema schema   = null;
            builder = new XSDBuilder();
            Reader reader = new InputStreamReader(is,"UTF-8");
            InputSource iSource = new InputSource(reader);

            if(iSource != null) {
              iSource.setEncoding("UTF-8");
              schema = builder.build(iSource);  //NOTE         
             }

            m_domParser.setXMLSchema(schema);

            Validator handler = new Validato开发者_如何转开发r();

            m_domParser.setErrorHandler(handler);

            //get the url for the xml file
            String url       = "file:" + new File(xmlFile).getAbsolutePath();
            m_domParser.parse(url);


        }

but at the NOTE comment (schema = builder.build(iSource);) during building it throws out an exception "invalid derivation from base type "missing derivation".

The XSD stream is being generated from the same xsd file why is it failing in the second place. Whilce building the XMLSchema, what is it meant by saying "invalid derivation from the base type"?

Please help me in understanding what went wrong in the second case.. Any quick responses are highly appreciated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜