2013-01-21 52 views
2

我有一個XML文件和一個XSD文件,我想根據XSD驗證XML。使用SAXParser驗證XML針對XSD導致錯誤

但我不斷收到以下錯誤:

org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document '/connector/connector.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. 

我打印的canonical path,以確保我試圖使用正確的文件。但它不會工作。

的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
    <Content> 
<commandLine> 
<commandCode>A1</commandCode> 
<marks><mark><code>mail</code><value>[email protected]</value></mark></marks> 
<customerID>1</customerID> 
<MessageType>2</MessageType> 
</commandLine> 
<Antwoordregel></Antwoordregel> 
</Content> 
</xs:schema> 

XSD:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
    <xs:element name="Content"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="commandLine" minOccurs="1" maxOccurs="1"> 
        <xs:complexType> 
         <xs:sequence minOccurs="1" maxOccurs="1"> 
          <xs:element name="commandCode" type="xs:string" minOccurs="1" maxOccurs="1"/> 
          <xs:element name="marks" minOccurs="0" maxOccurs="1"> 
           <xs:complexType> 
            <xs:sequence > 
             <xs:element name="mark" minOccurs="1" maxOccurs="unbounded"> 
              <xs:complexType> 
               <xs:sequence >                
                <xs:element name="code" type="xs:string" minOccurs="1" maxOccurs="1"/> 
                <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>                 
               </xs:sequence> 
              </xs:complexType> 
             </xs:element> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
          <xs:element name="customerID" type="xs:string" minOccurs="0" maxOccurs="1"/> 
          <xs:element name="MessageType" type="xs:string" minOccurs="0" maxOccurs="1"/> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
       <xs:element name="Antwoordregel" minOccurs="1" maxOccurs="1"> 
        <xs:complexType> 
         <xs:sequence minOccurs="0" maxOccurs="1"> 
          <xs:element name="resultCode" type="xs:string" minOccurs="0" maxOccurs="1"/> 
          <xs:element name="statusInfo" minOccurs="0" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element name="transactionInfo" type="xs:string" minOccurs="1" maxOccurs="1"/> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

的代碼我使用驗證:

static boolean validateAgainstXSD(String xml){ 
     try{ 
      File xsd = new File("connector/connector.xsd"); 
      System.out.println(xsd.getCanonicalPath()); 
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
      Schema schema = factory.newSchema(new StreamSource("/connector/connector.xsd")); 

      System.out.println(schema.toString()); 
      Validator validator = schema.newValidator(); 
      validator.validate(new StreamSource(xml)); 
      return true; 
     }catch(Exception exe){ 
      exe.printStackTrace(); 
     return false; 
     } 
    } 

這將始終返回false。我嘗試使用在線工具驗證XSD與XML,可以在這裏找到:www.utilities-online.info/xsdvalidation。此驗證器返回:

Not valid. 
Error - Line 2, 122: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'xs:schema'. 

我該如何解決此問題?

任何幫助表示讚賞,

謝謝!

回答

3

從XML文件中刪除第二行

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified"> 

正確的XML是

<?xml version="1.0" encoding="UTF-8"?> 
<Content> 
    <commandLine> 
     <commandCode>A1</commandCode> 
     <marks><mark><code>mail</code><value>[email protected]</value></mark></marks> 
     <customerID>1</customerID> 
     <MessageType>2</MessageType> 
    </commandLine> 
    <Antwoordregel></Antwoordregel> 
</Content> 

解決方案:

請檢查文件夾結構,並重新運行。

的源代碼:

package com.shashi.mpoole; 


import java.io.File; 

import javax.xml.XMLConstants; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 

public class XMLValid { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws Exception { 
     // TODO Auto-generated method stub 

     if(validateAgainstXSD(new File("connector/connector.xml"))) 
     { 
      System.out.println("Success"); 
     } 
     else 
     { 
      System.out.println("Failure"); 
     } 
    } 



    static boolean validateAgainstXSD(File xml){ 
     try{ 

      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
      Schema schema = factory.newSchema(new StreamSource("connector/connector.xsd")); 

      Validator validator = schema.newValidator(); 
      validator.validate(new StreamSource(xml)); 
      return true; 
     }catch(Exception exe){ 
      exe.printStackTrace(); 
     return false; 
     } 
    } 

} 

的文件夾結構

-----Project 

    ------------- src 

        -------------XMLValid.java 

    ------------- connector 

        -------------connector.xsd 

        -------------connector.xml 
+0

非常感謝!它現在使用網站進行驗證。但是我仍然在Java中得到相同的SAXParseException:'org.xml.sax.SAXParseException; schema_reference.4:無法讀取模式文檔'/connector/connector.xsd',因爲1)找不到文檔; 2)文件無法閱讀; 3)文檔的根元素不是。「你知道這是什麼原因嗎?再次感謝您的幫助! – Jef

+0

再次感謝,試着在我的代碼中實現它。但我仍然無法實現它的工作。得到了'MalformedURLException no protocol'。所以我已經將文件路徑更改爲'file://connector/connector.xsd,但我仍然收到相同的錯誤。 – Jef

+0

您是否檢查了文件夾結構 – Shashi

1

試試這個代碼來驗證你的XSD, 這個人是合適的標準,以避免你會得到什麼 除外(該一個是完整的證明代碼)。

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 

    <xs:element name="commandCode" type="xs:string"/> 
    <xs:element name="code" type="xs:string"/> 
    <xs:element name="value" type="xs:string" /> 
    <xs:element name="customerID" type="xs:string" /> 
    <xs:element name="MessageType" type="xs:string" /> 
    <xs:element name="resultCode" type="xs:string" /> 
    <xs:element name="transactionInfo" type="xs:string" /> 

<xs:element name="mark" > 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="code" minOccurs="1" maxOccurs="1"/> 
      <xs:element ref="value" minOccurs="1" maxOccurs="1"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="marks" > 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="mark" minOccurs="1" maxOccurs="unbounded" /> 
     <!--End element Mark --> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 
<xs:element name="commandLine"> 
    <xs:complexType> 
     <xs:sequence minOccurs="1" maxOccurs="1"> 
      <xs:element ref="commandCode" minOccurs="1" maxOccurs="1"/> 
      <xs:element ref="marks" minOccurs="0" maxOccurs="1"/> 
      <xs:element ref="customerID" minOccurs="0" maxOccurs="1"/> 
      <xs:element ref="MessageType" minOccurs="0" maxOccurs="1"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 
<xs:element name="statusInfo"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="transactionInfo" minOccurs="1" maxOccurs="1"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="Antwoordregel" > 
    <xs:complexType> 
     <xs:sequence minOccurs="0" maxOccurs="1"> 
      <xs:element ref="resultCode" minOccurs="0" maxOccurs="1"/> 
      <xs:element ref="statusInfo" minOccurs="0" maxOccurs="unbounded" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<!-- First Content Document Element --> 
<xs:element name="Content"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="commandLine" minOccurs="1" maxOccurs="1" /> 
       <xs:element ref="Antwoordregel" minOccurs="1" maxOccurs="1"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 
2

我能夠通過改變過的「XS:」命名空間前綴的XSD來解決這類問題,查找/替換所有實例是「XSD:」,而不是(也xmlns:xsd = definition)。然後,我將這個XSD託管在一個內部服務器上,並在XML中的schemaLocation屬性中指向,並且一切正常。

我想這是因爲精確措辭錯誤的:

  • 3)文檔的根元素不是< XSD:模式>

我知道,我知道,這不應該工作,但它確實。沒有翻譯的外部XSD對我無效http://camel.apache.org/schema/spring/camel-spring.xsd

我不確定這是否是SAX或Eclipse中的錯誤。