之路 on it Road.com
模式验证错误
针对 XML 的 XSD 验证不会总是成功。
很多时候,我们会收到验证错误。
这些错误将作为 SAXException抛出。
因此,捕获此异常,它将具有验证失败的上下文。
例如,我已经使用此更改更新了架构文件。
<xs:element name="firstName" type="xs:string" minOccurs="0"/> //to <xs:element name="firstName" type="xs:int" minOccurs="0"/>
现在看看验证错误。
javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; systemId: file:/C:/Users/JackLi/workspace/App/employee.xml; lineNumber: 7; columnNumber: 34; cvc-datatype-valid.1.2.1: 'JackLi' is not a valid value for 'integer'.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189) at com.onitroad.demo.JaxbExample.jaxbXmlFileToObject(JaxbExample.java:45) at com.onitroad.demo.JaxbExample.main(JaxbExample.java:23) Caused by: org.xml.sax.SAXParseException; systemId: file:/C:/Users/JackLi/workspace/App/employee.xml; lineNumber: 7; columnNumber: 34;
XSD 验证后将 XML 转换为 Java 对象
package com.onitroad.demo;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import com.onitroad.demo.model.Employee;
public class JaxbExample
{
public static void main(String[] args)
{
String xmlFile = "employee.xml";
String xsdFile = "employee.xsd";
jaxbXmlFileToObject(xmlFile, xsdFile);
}
private static void jaxbXmlFileToObject(String xmlFile, String xsdFile) {
JAXBContext jaxbContext;
try
{
//获取 JAXBContext
jaxbContext = JAXBContext.newInstance(Employee.class);
//创建 Unmarshaller
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// 设置验证器
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema employeeSchema = sf.newSchema(new File(xsdFile));
jaxbUnmarshaller.setSchema(employeeSchema);
//Unmarshal xml 文件
Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new File(xmlFile));
System.out.println(employee);
}
catch (JAXBException | SAXException e)
{
e.printStackTrace();
}
}
}
测试使用的employee.xml和employee.xsd的内容如下。
<employees> <employee id="101"> <name>JackLi Gupta</name> <title>Author</title> </employee> <employee id="102"> <name>BobRobert Lara</name> <title>Cricketer</title> </employee> </employees>
<?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="department" type="department"/>
<xs:element name="employee" type="employee"/>
<xs:complexType name="employee">
<xs:sequence>
<xs:element ref="department" minOccurs="0"/>
<xs:element name="firstName" type="xs:string" minOccurs="0"/>
<xs:element name="id" type="xs:int" minOccurs="0"/>
<xs:element name="lastName" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="department">
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
日期:2020-09-17 00:09:28 来源:oir作者:oir
