我正在使用 Jaxb 2.x 并尝试使用以下教程使用给定的 XSD 验证 XML 文档
她的是我写的代码
unmarshaller.setSchema(schema);
SAXSource source = new SAXSource(new InputSource(xmlFileLocation));
Validator validator = schema.newValidator();
validator.setErrorHandler(new XMLErrorHandler<Object>());
try {
validator.validate(source);
} catch (SAXException e) {
我的 XMLErrorHanlder 类具有以下签名
public class XMLErrorHandler<T> implements ErrorHandler {
public void error(SAXParseException exception) throws SAXException {
xmlUnmarshaller.setValidationFlag(true);
log.error(
"Line:Col[" + exception.getLineNumber()
+ ":" + exception.getColumnNumber()
+ "]:" + exception.getMessage());
exception.printStackTrace();
}
}
}
警告和致命代码现在已删除,它使用 XSD 验证 XML,但它只显示第一个遇到的错误,而我想在 colsole 上打印所有错误和控制台上的警告
我不确定我在哪里做错了这方面的任何帮助都会有所帮助
Edit1 这里是 XSD 文件的一部分
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="destination" type="Destination"/>
<xs:complexType name="Destination">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="destinationID" type="xs:string" minOccurs="0"/>
<xs:element name="shortDescription" type="xs:string" minOccurs="0"/>
<xs:element name="longDescription" type="xs:string" minOccurs="0"/>
<xs:element name="stateID" type="xs:string"/>
<xs:element name="typeCode" type="xs:int"/>
<xs:element name="countryCode" type="xs:string"/>
<xs:element name="categories" type="xs:string"/>
<xs:element name="transport" type="Transport" minOccurs="0" maxOccurs="1"/>
<xs:element name="cultures" type="Cultures" minOccurs="0"/>
<xs:element name="events" type="Events" minOccurs="0" maxOccurs="1"/>
<xs:element name="placesToVisit" type="PlacesToVisit" minOccurs="0" maxOccurs="1"/>
<xs:element name="contacts" type="Contact" minOccurs="0" maxOccurs="1"/>
<xs:element name="addresses" type="address" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
XML文件是
<destination xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="destination.xsd">
<name>Palampur</name>
<destinationID>PLP</destinationID>
<shortDescription>shortDescription</shortDescription>
<longDescription>longDescription</longDescription>
<typeCode>0</typeCode>
<categories>categories</categories>
在进行了一些研发之后,我的假设是 XSD 结构或生成的 XML 存在一些问题,但我不确定