如何使用 oracle.xml.parser.v2.DOMParser 使用 XSD InputStream 验证 XML

发布于 2024-10-06 16:58:15 字数 2122 浏览 0 评论 0原文

我正在尝试根据 XSD 文档验证 XML 文件。我正在采取两种方法。

  1. XML 和 XSD 都是文件

  2. XML 是一个文件,XSD 是一个流

对于 XML 和XSD 是文件,

  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

         }

}

它工作正常并且验证正确。这是我编写的用于验证使用 XSD 信息作为流的代码

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 Validator();

            m_domParser.setErrorHandler(handler);

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


        }

,但在构建期间的注释注释 (schema = builder.build(iSource);) 中,它抛出异常“来自基本类型的无效派生”缺少派生< /strong>”。XSD

流是从同一个 xsd 文件生成的,为什么它在第二个地方失败。在构建 XMLSchema 时,“从基本类型派生无效”是什么意思?

请帮助我理解第二种情况出了什么问题......任何快速反应都将受到高度赞赏。

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 Validator();

            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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文