首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXB-ElipseLink:封送处理程序不验证

JAXB-ElipseLink:封送处理程序不验证
EN

Stack Overflow用户
提问于 2012-01-19 10:15:17
回答 1查看 1.3K关注 0票数 1

我希望Eclipselink 2.3 Marshaller在编组时执行验证。我已经确保Schema是由SchemaFactory正确创建的,我将它传递给Marshaller.setSchema,并通过Marshaller.setEventHandler()注册了一个处理程序。

封送结果显然是无效的acc。对于它的架构(在Eclipse中验证),我可以看到我在handleEvent(ValidationEvent event)中的断点从未被击中。

我正在使用marshal(Object, XMLStreamWriter)编组XML片段,并希望Marshaller根据我通过的架构对这些片段执行验证。

有人知道为什么不发生这种事吗?

编辑

应该发生的验证错误:元素中缺少两个属性。

元素对应于包含在List<>中的Java。我正在使用以下方法编组清单:

代码语言:javascript
复制
<xml-element java-attribute="listInstance" xml-path="ListWrapperElement/ListElement" type="foo.ElementType" container-type="java.util.ArrayList"/>

元素本身的映射:

代码语言:javascript
复制
<java-type name="foo.ElementType" xml-accessor-type="PROPERTY">
    <java-attributes>
        // just <xml-attribute> elements here
    </java-attributes>
</java-type>

因此,所有属性都被编组到ListWrapperElement/ListElement/@attribute。其中2项缺失,未通过验证检测到。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-19 10:45:47

我无法重现你所看到的问题。下面是我尝试过的(从下面的博客文章中改编):

  • http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html

MarshalDemo (改编自博客帖子)

代码语言:javascript
复制
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);
    }

}

输出

代码语言:javascript
复制
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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8924293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档