gpt4 book ai didi

java - JAXB 2.0 验证不起作用

转载 作者:行者123 更新时间:2023-12-01 15:58:55 25 4
gpt4 key购买 nike

我正在开发 JAXB 2.0,目前正在验证部分,因为它没有按预期工作。下面是验证码

public void validateXMLToSchema(Unmarshaller ummarshaller,String xsdFileName) throws SAXException, JAXBException{
System.out.println(getClass().getResource(DEFAULT_XSD_NAME).toString());
Schema schema;
SchemaFactory schemaFactory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
if(null==xsdFileName)
schema=schemaFactory.newSchema(getClass().getResource(DEFAULT_XSD_NAME));

else
schema=schemaFactory.newSchema(new File(xsdFileName));

ummarshaller.setSchema(schema);
ummarshaller.setEventHandler( new ValidationEventHandler() {

@Override
public boolean handleEvent(ValidationEvent validationevent) {
if(validationevent.getSeverity()==ValidationEvent.FATAL_ERROR || validationevent.getSeverity()==ValidationEvent.ERROR || validationevent.getSeverity()==ValidationEvent.WARNING){
ValidationEventLocator locator = validationevent.getLocator();
log.info("Line:Col[" + locator.getLineNumber()
+ ":" + locator.getColumnNumber()
+ "]:" + validationevent.getMessage());
}
return true;
}
});

}

这是对该方法的调用

Destination destination=new Destination();
try {
destination=(Destination)unmarshal(Destination.class,new FileInputStream(new File("C:/Users/Raisonne/Desktop/jaxb/jaxb-ri-20101119/bin/destination.xml")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
System.out.println(destination.getName());

}

public static <T> T unmarshal( Class<T> docClass, InputStream inputStream )
throws JAXBException, SAXException {
String packageName = docClass.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance( packageName );
Unmarshaller u = jc.createUnmarshaller();
XMLValidator xmlValidator=new XMLValidator();
xmlValidator.validateXMLToSchema(u, null);

根据 XSD,我有几个字段作为必填字段,但即使删除它们,它也会给我错误,而它没有提供任何内容并将我的 xml 文件解析为相应的对象任何人都可以指出出了什么问题吗?

这是XSD的部分

    <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="culture" type="Culture" minOccurs="0" maxOccurs="1"/>
<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>
</xs:schema>

以及生成的Java文件

@XmlElement(required = true)
protected String name;
protected String destinationID;
protected String shortDescription;
protected String longDescription;
@XmlElement(required = true)
protected String stateID;

我正在从 xml 文件中删除 stateID,但验证部分仍然没有警报

提前致谢

最佳答案

你的代码片段有点难以理解,解码实际上发生了吗?您可能需要将最后一行添加到解码方法中:

public static <T> T unmarshal( Class<T> docClass, InputStream inputStream ) throws JAXBException, SAXException {
String packageName = docClass.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance( packageName );
Unmarshaller u = jc.createUnmarshaller();
XMLValidator xmlValidator=new XMLValidator();
xmlValidator.validateXMLToSchema(u, null);
u.unmarshal(inputStream);
}

关于java - JAXB 2.0 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4399585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com