2013-11-22 91 views
1

HI我正在嘗試使用JDOM驗證xml使用xsd。 這裏是我的代碼進行驗證: -使用JDOM驗證xml使用xsd

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: -

<?xml version="1.0" encoding="UTF-8"?> 


<document> 
<testCaseDataName> 
    <id>1</id > 
    <testCaseName>Edit</testCaseName > 
    <expectedResult>true</expectedResult > 

</testCaseDataName> 
<testCaseDataName> 
    <id>2</id > 
    <testCaseName>add</testCaseName > 
    <expectedResult>false</expectedResult> 
    <parameter> 
    <key>featues</key > 
    <value>w,f</value> 
    </parameter> 
</testCaseDataName> 

    <testCaseDataName> 
    <id>3</id > 
    <testCaseName>delete</testCaseName > 
    <expectedResult>duplicate</expectedResult> 
    <parameter> 
    <key>projectType</key > 
    <value>new</value> 
    <key>Name</key > 
    <value>mashnew</value> 
    <key>status</key > 
    <value>ACTIVE</value> 
    <key>canOrder</key > 
    <value>Yes</value> 
    </parameter> 
</testCaseDataName> 
</document> 

我file.xsd: -

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/ACL" 
    xmlns:tns="http://www.example.org/ACL" 
    elementFormDefault="qualified"> 
    <include schemaLocation=""></include> 
    <element name="document"> 
    <complexType> 
     <sequence> 
    <element name="testCaseDataName"> 
    <complexType> 
     <sequence> 
     <element name ="id" type ="ID" ></element> 
     <element name ="testCaseName" type ="string" ></element> 
     <element name = "expectedResult" type="string"></element> 
     <element name = "parameter" minOccurs="0"> 
     <complexType> 
      <sequence> 
       <element name="key" type ="string" maxOccurs="1"></element> 
       <element name="value" type="string" maxOccurs="unbounded"></element> 
      </sequence> 
     </complexType> 
     </element> 
     </sequence> 
    </complexType> 
    </element> 
    </sequence> 
    </complexType> 
    </element> 
</schema> 

當我運行這段代碼我得到的跟隨錯誤。任何人都可以請幫我解決這個問題。

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 

回答

1

這是做驗證可以請你檢查你的代碼

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")); 

使用下面的架構

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="document"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="testCaseDataName" maxOccurs="unbounded" minOccurs="0"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element type="xs:byte" name="id"/> 
       <xs:element type="xs:string" name="testCaseName"/> 
       <xs:element type="xs:string" name="expectedResult"/> 
       <xs:element name="parameter" minOccurs="0"> 
       <xs:complexType> 
        <xs:choice maxOccurs="unbounded" minOccurs="0"> 
        <xs:element type="xs:string" name="key"/> 
        <xs:element type="xs:string" name="value"/> 
        </xs:choice> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
+0

我仍然得到相同的錯誤。你可以看看我的xml和xmd是否正確? – Newbie

+0

讓我試試我會看看 – constantlearner

+0

你可以嘗試新的模式 – constantlearner