我有一个 XML 文件和一个 XSD 文件,我想根据 XSD 验证 XML。
但我不断收到以下错误:
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>test@test.com</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'.
我能做些什么来解决这个问题?
任何帮助表示赞赏,
谢谢!