2014-09-01 89 views
1

我目前正在與編寫一些單元測試以檢查XSD是否對示例XML文件有效有關的問題。我收到以下錯誤:根據XML驗證多個XSD

org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'VMS'. 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325) 
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906) 
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746) 
    at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:277) 
    at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:244) 
    at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:190) 
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:109) 

我的XSD文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <!-- Messages header --> 

    <xs:complexType name="VMS_TYPE"> 
     <!-- Recipient of message --> 
     <xs:attribute name="TO" type="xs:string"/> 
     <!-- Sender of message --> 
     <xs:attribute name="FR" type="xs:string" /> 
     <!-- Message identification number consists --> 
     <xs:attribute name="VN" type="xs:string" /> 
     <!-- Message creation date --> 
     <xs:attribute name="OD" type="xs:date" /> 
     <!-- Message creation time --> 
     <xs:attribute name="OT" type="xs:string" /> 
     <!-- Mean of communication to sender of message --> 
     <xs:attribute name="MTO" type="xs:string" /> 
     <!-- Mean of communication to recipient of message --> 
     <xs:attribute name="MFR" type="xs:string" /> 
     <!-- TODO - explain attribute meaning --> 
     <xs:attribute name="MD" type="xs:string" /> 
    </xs:complexType> 

</xs:schema> 

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:include schemaLocation="vms_header.xsd" /> 

    <!-- Alarm message --> 
    <xs:complexType name="ALR_TYPE" > 
     <xs:complexContent> 
      <xs:extension base="VMS_TYPE"> 
       <xs:sequence> 
        <xs:element name="ALR" type="ALR_BODY_TYPE" /> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="ALR_BODY_TYPE"> 
      <xs:attribute name="BAT" type="xs:int"/> 
      <xs:attribute name="TEM" type="xs:int"/> 
      <xs:attribute name="GPS" type="xs:int"/> 
      <xs:attribute name="SOS" type="xs:int"/> 
      <xs:attribute name="CV" type="xs:int"/> 
      <xs:attribute name="MOD" type="xs:int"/> 
      <xs:attribute name="FGPS" type="xs:int"/> 
      <xs:attribute name="FBAT" type="xs:int"/> 
      <xs:attribute name="FALM" type="xs:int"/> 
      <xs:attribute name="FSOS" type="xs:int"/> 
      <xs:attribute name="FFW" type="xs:int"/> 
      <xs:attribute name="ASOS" type="xs:int"/> 
    </xs:complexType> 

</xs:schema> 

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:include schemaLocation="types/alr_msg_type.xsd"/> 

    <xs:element name="VMS" type="ALR_TYPE"/> 

</xs:schema> 

我的XML樣本文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<VMS TO="CV0001" FR="CAR001" VN="CAR001201406040001" OD="2014-06-04" OT="12:14" MD="+3465104505050"> 
    <ALR BAT="0" TEM="0" GPS="0" SOS="0" CV="0" MOD="0" FGPS="0" FBAT="0" FALM="0" FSOS="0" FFW="0" ASOS="0"/> 
</VMS> 
+0

得到任何Java代碼? – laune 2014-09-01 10:28:44

回答

1

這對我的作品,jdk1.8.0_20

./third.xsd # probably not the real name, but you no tell 
./types/alr_msg_type.xsd 
./types/vms_header.xsd 

public static void main(String[] args) throws Exception { 
    Handler handler = new Handler(); 
    DocumentBuilder parser = 
    DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
    parser.setErrorHandler(handler); 
    try { 
    Document document = parser.parse(new File("x.xml")); 
    SchemaFactory factory = 
     SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Source schemaFile = new StreamSource(new File("third.xsd")); 
    Schema schema = factory.newSchema(schemaFile); 
    Validator validator = schema.newValidator(); 
    validator.setErrorHandler(handler); 
    // validate the DOM tree 
    try { 
     validator.validate(new DOMSource(document)); 
    } catch (SAXException e) { 
     // ... 
     System.out.println("VAlidation error"); 
    } 
    } catch (SAXParseException e) { 
    // syntax error in XML document  
    System.out.println("Syntax error"); 
    } 
} 
0

我用下面的代碼(與JDK 1.8.0_11):

DefaultHandler handler = new DefaultHandler(); 
     DocumentBuilder parser = 
       DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     parser.setErrorHandler(handler); 
     try { 
      Document document = parser.parse(getClass().getResourceAsStream("x.xml")); 
      SchemaFactory factory = 
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
      Source schemaFile = new StreamSource(getClass().getResourceAsStream("third.xsd")); 
      Schema schema = factory.newSchema(schemaFile); 
      Validator validator = schema.newValidator(); 
      validator.setErrorHandler(handler); 
      // validate the DOM tree 
      try { 
       validator.validate(new DOMSource(document)); 
      } catch (SAXException e) { 
       // ... 
       System.out.println("Validation error"); 
      } 
     } catch (SAXParseException e) { 
      // syntax error in XML document 
      System.out.println("Syntax error"); 
      System.out.println(e); 
     } 

結果是:

語法錯誤 org.xml.sax.SAXParseException; lineNumber:6; columnNumber:45; src-resolve:無法將名稱'ALR_TYPE'解析爲(n)'類型定義'組件。

+0

好吧,我添加了其他.xsd文件,它工作正常。謝謝你 – 2014-09-01 11:53:00