我在使用 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)