0

我在使用 B2MML 标准中的一种 XSD 模式(更具体地说是材料模式)验证 XML 文件时遇到了一些问题。我们的代码可以与其他 XSD 一起使用,但它不能与 B2MML 一起使用。我们处理验证的方法如下:

public class XMLvalidate {

     public static void Xml2String(String xmlFile){
        // Danner JAVA DOM XML Parser

        DocumentBuilderFactory builderFactory =
               DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = null;
       try {
           builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  
        }

       // Changes the XML file to XML DOM standard

       try {
           Document document = builder.parse(
                   new FileInputStream(new File(xmlFile)));
           Transformer transformer = TransformerFactory.newInstance().newTransformer();
           transformer.setOutputProperty(OutputKeys.INDENT, "yes");

           // Make the DOM XML file readable
           StreamResult result = new StreamResult(new StringWriter());
          DOMSource source = new DOMSource(document);
          transformer.transform(source, result);
           String xmlString = result.getWriter().toString();
           System.out.println(xmlString);
       } catch (SAXException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   }

 public static void XMLValidator(String xmlFile, String validationFile)throws SAXException, IOException{


        String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
         SchemaFactory factory = SchemaFactory.newInstance(language);
        ((factory.newSchema(new File(validationFile))).newValidator()).validate(new StreamSource(new File(xmlFile)));


    }

 public static void main(String [] args){
     boolean flag = true;
     try{

            try{XMLValidator("src\\Material.xml", "src\\B2MML-V0600-Material.xsd");}
            catch (SAXException e){ flag = false; } 
            catch (IOException e) { flag = false; }
            System.out.println("XML fil is valid : " + flag);
 }
     catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}

链接到 B2MML:https ://github.com/Rubatharisan/radis/blob/master/B2MML-V0600-Material.xsd 链接到我们的 XML 文件:https ://github.com/Rubatharisan/radis/blob/master/材料.xml

我们的 XML 文件是通过 XSD 文件自动生成的。XML间谍。

为什么它不适用于 B2MML?提前致谢。

错误:

我再次查看了我的程序,我在之前的评论中犯了一个错误。出现此错误:

org.xml.sax.SAXParseException; systemId: file:/C:/Users/Bj%C3%B8rn/Desktop/Workspace2/XMLValidator/src/B2MML-V0600-Material.xsd; lineNumber: 183; columnNumber: 87; src-resolve: Cannot resolve the name 'Extended:MaterialInformation' to a(n) 'group' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDGroupTraverser.traverseLocal(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractParticleTraverser.traverseSeqChoice(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractParticleTraverser.traverseSequence(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseGlobal(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseGlobalDecl(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseGlobal(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
at javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
at javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
at XMLvalidate.XMLValidator(XMLvalidate.java:73)
at XMLvalidate.main(XMLvalidate.java:82)
4

1 回答 1

0

错误消息表明验证器没有成功找到组Extended:MaterialInformation。没有任何进一步调查,对此最可能的解释是它没有成功找到该名称空间的模式文档,该名称空间是使用相对 URI 给出的B2MML-V0600-AllExtensions.xsd

从错误消息的措辞看来,您正在使用 的本地副本B2MML-V0600-Material.xsd,因此验证器正在使用本地副本的基本 URI 解析相对引用,并在file:/C:/Users/Bj%C3%B8rn/Desktop/Workspace2/XMLValidator/src/B2MML-V0600-AllExtensions.xsd. 当您制作顶级模式文档的本地副本时,您是否还在正确位置制作了它通过相对引用引用的所有模式文档的本地副本?同样,对于他们自己通过相对引用导入、包含或重新定义的所有模式文档?等等递归?如果是这样,那么本地副本的组织可能有问题;如果没有,那么现在你知道你需要做什么了。

于 2013-11-15T16:15:42.060 回答