java - XML 架构验证问题

标签 java xml xsd xml-validation

我正在尝试根据我之前编写的 XSD Schema 验证一个 XML 文件。验证我的 xml 文件的 java 代码如下所示。当我尝试验证 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;
    }
}

最佳答案

您的命名空间在 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>

关于java - XML 架构验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497685/

相关文章:

java.lang.ClassNotFoundException : com. twilio.type.Endpoint tomcat 问题

objective-c - SVGKit 不显示 potrace 生成的图像

java - 在java中使用vtd-xml获取xml中的属性文本

regex - XSD 正则表达式模式 : this or nothing

xsd - 从 XSD 生成 DDIC 结构?

java - 分割字符串时出现异常

java - 使用 Apache-POI 设置选项卡大小

java - 策略设计模式

c# - C#中通过XmlSerializer类反序列化多个同名XML元素

java - 模式验证安卓