私はこのxsdを持っています:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="shiporder">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="orderperson" type="xsd:string"/>
<xsd:element name="shipto">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="item" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="note" type="xsd:string" minOccurs="0"/>
<xsd:element name="quantity" type="xsd:positiveInteger"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="orderid" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
これは、スキーマを作成するためのコードです。
String xsd = "..."
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
validator.validate(xmlFile);
} catch (SAXException e) {
System.out.println("Reason: " + e.getLocalizedMessage());
} catch (IOException e) {
System.out.println("Reason: " + e.getLocalizedMessage());
}
新しいスキーマ インスタンスの作成中に次のエラーが発生します。
SCHEMA : schema_reference.4: Failed to read schema document '...', 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>.
文字列 xsd は、上記の xsd ファイルを表します。
どうすれば問題を解決できますか? 何か案が?提案?
編集:私はこの行を代用して解決しました:
Schema schema = schemaFactory.newSchema(new StreamSource(xsd));
これとともに:
Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(xsd)));