我希望Eclipselink 2.3 Marshaller在编组时执行验证。我已经确保Schema是由SchemaFactory正确创建的,我将它传递给Marshaller.setSchema,并通过Marshaller.setEventHandler()注册了一个处理程序。
封送结果显然是无效的acc。对于它的架构(在Eclipse中验证),我可以看到我在handleEvent(ValidationEvent event)中的断点从未被击中。
我正在使用marshal(Object, XMLStreamWriter)编组XML片段,并希望Marshaller根据我通过的架构对这些片段执行验证。
有人知道为什么不发生这种事吗?
编辑
应该发生的验证错误:元素中缺少两个属性。
元素对应于包含在List<>中的Java。我正在使用以下方法编组清单:
<xml-element java-attribute="listInstance" xml-path="ListWrapperElement/ListElement" type="foo.ElementType" container-type="java.util.ArrayList"/>元素本身的映射:
<java-type name="foo.ElementType" xml-accessor-type="PROPERTY">
<java-attributes>
// just <xml-attribute> elements here
</java-attributes>
</java-type>因此,所有属性都被编组到ListWrapperElement/ListElement/@attribute。其中2项缺失,未通过验证检测到。
发布于 2012-01-19 10:45:47
我无法重现你所看到的问题。下面是我尝试过的(从下面的博客文章中改编):
MarshalDemo (改编自博客帖子)
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.eclipse.persistence.Version;
public class MarshalDemo {
public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.setName("Jane Doe");
customer.getPhoneNumbers().add(new PhoneNumber());
customer.getPhoneNumbers().add(new PhoneNumber());
customer.getPhoneNumbers().add(new PhoneNumber());
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("src/blog/jaxb/validation/customer.xsd"));
JAXBContext jc = JAXBContext.newInstance(Customer.class);
System.out.println(jc.getClass());
System.out.println(Version.getVersion());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setSchema(schema);
marshaller.setEventHandler(new MyValidationEventHandler());
XMLStreamWriter xsw = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
marshaller.marshal(customer, xsw);
}
}输出
class org.eclipse.persistence.jaxb.JAXBContext
2.3.0
EVENT
SEVERITY: 1
MESSAGE: cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringWithMaxSize5'.
LINKED EXCEPTION: org.eclipse.persistence.oxm.record.ValidatingMarshalRecord$MarshalSAXParseException: cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringWithMaxSize5'.
LOCATOR
LINE NUMBER: -1
COLUMN NUMBER: -1
OFFSET: -1
OBJECT: forum8924293.Customer@ef2c60
NODE: null
URL: null
EVENT
SEVERITY: 1
MESSAGE: cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
LINKED EXCEPTION: org.eclipse.persistence.oxm.record.ValidatingMarshalRecord$MarshalSAXParseException: cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
LOCATOR
LINE NUMBER: -1
COLUMN NUMBER: -1
OFFSET: -1
OBJECT: forum8924293.Customer@ef2c60
NODE: null
URL: null
EVENT
SEVERITY: 1
MESSAGE: cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element '{phone-number}' is expected at this point.
LINKED EXCEPTION: org.eclipse.persistence.oxm.record.ValidatingMarshalRecord$MarshalSAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element '{phone-number}' is expected at this point.
LOCATOR
LINE NUMBER: -1
COLUMN NUMBER: -1
OFFSET: -1
OBJECT: forum8924293.Customer@ef2c60
NODE: null
URL: null
<?xml version="1.0"?><customer><name>Jane Doe</name><phone-number></phone-number><phone-number></phone-number><phone-number></phone-number></customer>https://stackoverflow.com/questions/8924293
复制相似问题