如何用多个schema文件验证一个xml文件?
现在有七个相互关联的schema文件,我想用这几个文件来验证一个xml文件的是否符合规则,在网上找到一种方法但老是不对!!!
package net.aidingwei.util; import java.io.*; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.*; import org.xml.sax.SAXException; public class xmlValidation { /** * @功能描述 TODO * @创建人 - 017 * @创建时间 2013-5-23 下午5:58:38 * @param args * @throws SAXException * @throws IOException */ public static void main(String[] args) throws SAXException, IOException { // 1. Specify you want a factory for RELAX NG SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); // 2. Load the specific schema you want. // Here I load it from a java.io.File, but we could also use a // java.net.URL or a javax.xml.transform.Source File CDAFile = new File("F:\test\schema\sdschemas\CDA.xsd"); File PCHISFile = new File("F:\test\schema\sdschemas\PCHIS.xsd"); File POCDFile = new File("F:\test\schema\sdschemas\POCD_MT000040.xsd"); File datatypesFile = new File("F:\test\schema\CoreSchemas\datatypes.xsd"); File datatypesBaseFile = new File("F:\test\schema\CoreSchemas\datatypes-base.xsd"); File NarrativeBlockFile = new File("F:\test\schema\CoreSchemas\NarrativeBlock.xsd"); File vocFile = new File("F:\test\schema\CoreSchemas\voc.xsd"); Source[] source = { new StreamSource(CDAFile), new StreamSource(PCHISFile), new StreamSource(POCDFile), new StreamSource(datatypesFile), new StreamSource(datatypesBaseFile), new StreamSource(NarrativeBlockFile), new StreamSource(vocFile) }; // 3. Compile the schema. Schema schema = factory.newSchema(source); // 4. Get a validator from the schema. Validator validator = schema.newValidator(); // 5. Parse the document you want to check. String input = "F:\test\201305\0721ee956c104d32a72beb052bd529d6.xml"; Source source2 = new StreamSource(input); // 6. Check the document try { validator.validate(source2); System.out.println(input + " is valid."); } catch (SAXException ex) { System.out.println(input + " is not valid because "); System.out.println(ex.getMessage()); } } } 总是报错 Exception in thread "main" org.xml.sax.SAXParseException: rcase-Recurse.2: There is not a complete functional mapping between the particles. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384) at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.reportSchemaError(XSConstraints.java:276) at com.sun.org.apache.xerces.internal.impl.xs.XSConstraints.fullSchemaChecking(XSConstraints.java:405) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:530) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:489) at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:224) at net.aidingwei.util.xmlValidation.main(xmlValidation.java:47)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
建议首先测试下单个xsd文件能否正确验证,瘦减下结构,看看怎么样,是否仍然报错?
建议首先测试下单个xsd文件能否正确验证,瘦减下结构,看看怎么样,是否仍然报错?
建议首先测试下单个xsd文件能否正确验证,瘦减下结构,看看怎么样,是否仍然报错?
建议首先测试下单个xsd文件能否正确验证,瘦减下结构,看看怎么样,是否仍然报错?
建议首先测试下单个xsd文件能否正确验证,瘦减下结构,看看怎么样,是否仍然报错?
这么多阅,就没人给点指导么?