XML解析器和验证器 - B2MML

时间:2013-11-15 11:09:57

标签: java xml xsd

我在使用B2MML标准中的一个XSD架构(更具体地说是Materials-schema)验证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/Material.xml

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

为什么它不适用于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)

1 个答案:

答案 0 :(得分: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的架构文档。当您创建顶级架构文档的本地副本时,是否还通过相对引用在正确的位置制作了它引用的所有架构文档的本地副本?类似地,对于所有架构文档,它们自己通过相对引用导入,包含或重新定义?等等递归?如果是这样,那么在组织本地副本时可能会出现问题;如果没有,那么现在你知道你需要做什么。