java - w3schools xsd 示例不适用于 dom4j。如何使用 dom4j 通过 xsds 验证 xml?

标签 java xsd dom4j

我正在尝试使用 dom4j 验证 http://www.w3schools.com/Schema/schema_example.asp 处的 xml使用同一页面中的 xsd。它失败并出现以下错误:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'shiporder'.

我使用以下代码:

SAXReader reader = new SAXReader();
reader.setValidation(true);
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
reader.setErrorHandler(new XmlErrorHandler());
reader.read(in);

其中 in 是一个 InputStream,XmlErrorHandler 是一个简单的类,仅记录所有错误。

我正在使用以下 xml 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test1.xsd">
  <orderperson>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
  </item>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder>

以及相应的xsd:

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

<xs:simpleType name="stringtype">
  <xs:restriction base="xs:string"/>
</xs:simpleType>

<xs:simpleType name="inttype">
  <xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>

<xs:simpleType name="dectype">
  <xs:restriction base="xs:decimal"/>
</xs:simpleType>

<xs:simpleType name="orderidtype">
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{6}"/>
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="shiptotype">
  <xs:sequence>
    <xs:element name="name" type="stringtype"/>
    <xs:element name="address" type="stringtype"/>
    <xs:element name="city" type="stringtype"/>
    <xs:element name="country" type="stringtype"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="itemtype">
  <xs:sequence>
    <xs:element name="title" type="stringtype"/>
    <xs:element name="note" type="stringtype" minOccurs="0"/>
    <xs:element name="quantity" type="inttype"/>
    <xs:element name="price" type="dectype"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="shipordertype">
  <xs:sequence>
    <xs:element name="orderperson" type="stringtype"/>
    <xs:element name="shipto" type="shiptotype"/>
    <xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
  </xs:sequence>
  <xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>

<xs:element name="shiporder" type="shipordertype"/>

</xs:schema>

xsd 和 xml 文件位于同一目录中。有什么问题吗?

最佳答案

我怀疑是因为这个:

reader.read(in);

您没有向读者提供有关输入流来自何处的任何上下文,因此它无法解析架构。尝试向读取器传递诸如 FileURL 对象之类的东西,它可以用来生成对架构的相对引用。

关于java - w3schools xsd 示例不适用于 dom4j。如何使用 dom4j 通过 xsds 验证 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2518904/

相关文章:

java - 日历 week_of_year 来到 week_of_month

java - 动态根元素 JAXB?

Java dom4j org/jaxen/NamespaceContext 异常

java - javabean 中的日期或字符串声明

java - 对于大于 150kb 的文件上传请求参数为 null,多部分文件上传,Spring 3.2,wildfly 9.0.0

xml - 限制 xsd 类型 :any to xsd:string only?

xsd - 如何在 NuGet 包中包含配置部分的 XSD 文件?

java - 使用 dom4j 从流中读取单个 XML 文档

java - 如何使用 Dom4J 在 Java 中定义 XML 实体?

java - 防止在 Spring Hibernate 中存储重复值的正确方法