使用JDOM使用xsdvalidationxml

我试图使用JDOM使用xsdvalidationxml。 这是我的validation代码: –

public class jdom1 { public static void main(String[] args) throws JDOMException, IOException { // TODO Auto-generated method stub File xsdfile = new File("file.xsd"); XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile); SAXBuilder builder = new SAXBuilder(schemafac); Document doc = builder.build(new File("file.xml")); Element root = doc.getRootElement(); for(Element testCase : root.getChildren()){ //Code } } 

我的file.xml: –

    1 Edit true   2 add false  featues w,f    3 delete duplicate  projectType new Name mashnew status ACTIVE canOrder Yes    

我的file.xsd: –

                            

当我运行此代码时,我收到以下错误。 任何人都可以帮我解决这个问题。

 Exception in thread "main" org.jdom2.input.JDOMParseException: Error on line 4 of document file:/C:/Users/file.xml: cvc-elt.1: Cannot find the declaration of element 'document'. at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:228) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:277) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:264) at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116) at com.memoir.client.test.testdb.jdom1.main(jdom1.java:24) Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'document'. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217) ... 4 more 

这是validation的方法,请检查您的代码

 SchemaFactory schemafac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemafac.newSchema(new File("myschema.xsd")); XMLReaderJDOMFactory factory = new XMLReaderSchemaFactory(schema); SAXBuilder sb = new SAXBuilder(factory); Document doc = sb.build(new File("file.xml")); 

使用以下架构

                          

我的架构错了。 使用此http://www.freeformatter.com/xsd-generator.html生成架构,validation工作正常。 谢谢