2013-10-21 177 views
2

我正在嘗試驗證XML文件與我以前寫過的XSD Schema。下面顯示了驗證我的xml文件的java代碼。當我嘗試驗證XML時,我總是得到如下錯誤:「無法找到根元素的聲明」。XML模式驗證問題

你能幫我解決這個問題嗎?

XML文件

<?xml version="1.0" encoding="UTF-8"?> 
<AllBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://myNameSpace.com" 
     schemaLocation="http://myNameSpace.com book.xsd"> 
    <book> 
     <id>1</id> 
     <title>aşk ve gurur</title> 
     <author>james brown</author> 
     <category>science</category> 
     <availablity>100</availablity> 
     <price>5000</price> 
    </book> 
    <book> 
     <id>2</id> 
     <title>kskkdn</title> 
     <author>mşlfke</author> 
     <category>love</category> 
     <availablity>50</availablity> 
     <price>5000</price> 
    </book> 
</AllBooks> 

架構文件

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     targetNamespace="http://www.w3schools.com" 
     xmlns="http://www.w3schools.com" elementFormDefault="qualified"> 

    <xs:element name="AllBooks"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="book" maxOccurs="unbounded"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="id" type="xs:integer"/> 
          <xs:element name="title" type="xs:string"/> 
          <xs:element name="author" type="xs:string"/> 
          <xs:element name="category" type="xs:string"/> 
          <xs:element name="availability" type="xs:integer"/> 
          <xs:element name="price" type="xs:integer"/> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

和Java代碼

static boolean validateAgainstXSD(InputStream xml, InputStream xsd) 
{ 
    try 
    { 
     SchemaFactory factory = 
     SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
     Schema schema = factory.newSchema(new StreamSource(xsd)); 
     Validator validator = schema.newValidator(); 
     validator.validate(new StreamSource(xml)); 
     return true; 
    } 
    catch(Exception ex) 
    { 
     return false; 
    } 
} 

回答

1

您的命名空間d o在XSD和XML文件之間不匹配。另外,availability在XML文件中拼寫錯誤爲availablity。更正遵循...

使用此XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://myNameSpace.com" 
      elementFormDefault="qualified"> 

    <xs:element name="AllBooks"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="book" maxOccurs="unbounded"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="id" type="xs:integer"/> 
       <xs:element name="title" type="xs:string"/> 
       <xs:element name="author" type="xs:string"/> 
       <xs:element name="category" type="xs:string"/> 
       <xs:element name="availability" type="xs:integer"/> 
       <xs:element name="price" type="xs:integer"/> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

那麼這個固定的XML實例文檔將是有效的:

<?xml version="1.0" encoding="UTF-8"?> 
<AllBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://myNameSpace.com" 
      xsi:schemaLocation="http://myNameSpace.com book.xsd"> 
    <book> 
     <id>1</id> 
     <title>aşk ve gurur</title> 
     <author>james brown</author> 
     <category>science</category> 
     <availability>100</availability> 
     <price>5000</price> 
     </book> 
    <book> 
     <id>2</id> 
     <title>kskkdn</title> 
     <author>mşlfke</author> 
     <category>love</category> 
     <availability>50</availability> 
     <price>5000</price> 
    </book> 
</AllBooks> 
+0

非常感謝您,先生。 – Jason