我有一個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'.
我該如何解決此問題?
任何幫助表示讚賞,
謝謝!
非常感謝!它現在使用網站進行驗證。但是我仍然在Java中得到相同的SAXParseException:'org.xml.sax.SAXParseException; schema_reference.4:無法讀取模式文檔'/connector/connector.xsd',因爲1)找不到文檔; 2)文件無法閱讀; 3)文檔的根元素不是。「你知道這是什麼原因嗎?再次感謝您的幫助! –
Jef
再次感謝,試着在我的代碼中實現它。但我仍然無法實現它的工作。得到了'MalformedURLException no protocol'。所以我已經將文件路徑更改爲'file://connector/connector.xsd,但我仍然收到相同的錯誤。 – Jef
您是否檢查了文件夾結構 – Shashi