我在使用 Java 中的 XSD 验证库提供的错误消息文本时遇到问题。就我而言,似乎 Apache Xerces 是在后台使用的。我发现 Java 的错误至少非常具有误导性。
我从 Java 得到的错误是:
cvc-complex-type.2.4.b:元素“根”的内容不完整。需要“{subnode1, subnode2}”之一。
但是另一个 XML 编辑器 XMLSpy 2004 是这样说的:
'subnode1'之后的''中预期的强制性元素:subnode2,subnode3
第二个错误在这里似乎更准确,因为没有丢失 subnode1。但是,subnode2 和 subnode3 都是。
这是我的 XML 文件:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<subnode0/>
<subnode1/>
</root>
这是我使用的 XSD:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="NodeType">
</xs:element>
<xs:complexType name="NodeType">
<xs:sequence>
<xs:element name="subnode0" type="subnode0_Type" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="subnode1" type="subnode1_Type" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="subnode2" type="subnode2_Type" minOccurs="1" maxOccurs="1"/>
<xs:element name="subnode3" type="subnode3_Type" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="subnode0_Type">
</xs:complexType>
<xs:complexType name="subnode1_Type">
</xs:complexType>
<xs:complexType name="subnode2_Type">
</xs:complexType>
<xs:complexType name="subnode3_Type">
</xs:complexType>
</xs:schema>
这是我调用验证 API 的 Java 代码。
public class XsdErrorTextMain {
private final static DocumentBuilder documentBuilder;
private final static SchemaFactory schemaFactory;
static {
try {
final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (final ParserConfigurationException e) {
throw new RuntimeException(e);
}
schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
}
public static void main(final String[] args) throws Exception {
final Document xmlDocument = readXmlFile("file.xml");
final Schema schema = readXsdFile("schema.xsd");
runXmlValidation(xmlDocument, schema);
}
private static void runXmlValidation(final Document xmlDocument, final Schema schema) throws SAXException {
try {
final Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler() {
@Override
public void warning(final SAXParseException exception) {
return;
}
@Override
public void error(final SAXParseException exception) throws SAXException {
displayValidationError(exception);
}
@Override
public void fatalError(final SAXParseException exception) throws SAXException {
throw new RuntimeException("A fatal error was raised during the validation", exception);
}
});
validator.validate(new DOMSource(xmlDocument));
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
private static void displayValidationError(final SAXParseException validationError) {
System.out.println("Line: " + validationError.getLineNumber());
System.out.println("Column: " + validationError.getColumnNumber());
System.out.println("Message: " + validationError.getMessage());
}
private static Document readXmlFile(final String path) throws IOException, SAXException {
try (final InputStream xmlInputStream = ClassLoader.getSystemResource(path).openStream()) {
return documentBuilder.parse(xmlInputStream);
}
}
private static Schema readXsdFile(final String path) throws IOException, SAXException {
final Document xsdDocument = readXmlFile(path);
return schemaFactory.newSchema(new DOMSource(xsdDocument));
}
}
我在这里错过了什么吗?您知道其他可能更准确地计算错误消息的实现吗?任何输入表示赞赏。谢谢!