简体   繁体   English

marshal / unmarshal基于2种不同模式的2个不同的类

[英]marshal/unmarshal 2 different classes that are based on 2 different schemas

Im trying to configure a jaxb2Marshaller in spring beans config file but Im pretty new to Spring and JAXB so i might be going about it the wrong way. 我试图在spring beans配置文件中配置jaxb2Marshaller,但我对Spring和JAXB很新,所以我可能会采用错误的方式。

What i want to achieve is the same bean that will marshal/unmarshal 2 different classes that are based on 2 different schemas. 我想要实现的是同一个bean,它将编组/解组基于2个不同模式的2个不同的类。 Perhaps thats not possible because when i have both configured and run my tests they fail for the second class in the configuration (AccountResponse). 也许那是不可能的,因为当我配置并运行我的测试时,他们在配置(AccountResponse)中的第二个类失败了。

This is the XML configuration: 这是XML配置:

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="marshallerProperties">
        <map>
            <entry key="com.sun.xml.bind.namespacePrefixMapper">
                <bean id="NamespacePrefixMapperImpl" class="org.lp.soa.controller.xml.LpsNamespacePrefixMapper" />
            </entry>
        </map>
    </property>
    <property name="classesToBeBound">
        <list>                              
            <value>org.lp.soa.controller.data.request.AccountRequest</value>
            <value>org.lp.soa.controller.data.response.AccountResponse</value>
        </list>
    </property>     
    <property name="schemas">
        <list>
        <value>classpath:schema/AccountRequest.xsd</value>
        <value>classpath:schema/AccountResponse.xsd</value>
        </list>
    </property>
</bean>

if i comment out the AccountRequest.xsd value from the config and then run my tests again the marshal/unmarshal for the second class (AccountResponse) they all pass, if I then uncomment it I get the error: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'accountResponse'. 如果我从配置中注释掉AccountRequest.xsd值,然后再次运行我的测试,那么第二个类(AccountResponse)的编组/解组它们都会通过,如果我取消注释它,我会得到错误:org.xml.sax.SAXParseException :cvc-elt.1:找不到元素'accountResponse'的声明。

Am i going about it the wrong way? 我是以错误的方式去做的吗? Is it not supposed to be possible to handle two classes with two schemas? 难道不应该用两个模式处理两个类吗?

Thanks, Yoav. 谢谢,Yoav。

" if i comment out the AccountRequest.xsd value from the config and then run my tests again the marshal/unmarshal for the second class (AccountResponse) they all pass, if I then uncomment it I get the error: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'accountResponse'. " “如果我从配置中注释掉AccountRequest.xsd值,然后再次运行我的测试,那么第二个类(AccountResponse)的编组/解组它们都会通过,如果我取消注释它,我会得到错误:org.xml.sax。 SAXParseException:cvc-elt.1:找不到元素'accountResponse'的声明。“

Sounds like the Schema object being created by SchemaFactory.newSchema() is only processing the first xsd in the list. 听起来像SchemaFactory.newSchema()创建的Schema对象只处理列表中的第一个xsd。

If you have multiple schema files that are in the same namespace (targetNamespace?) then It could be this bug that is causing trouble: 如果你有多个模式文件在同一个命名空间(targetNamespace?),那么可能是这个bug造成了麻烦:

https://issues.apache.org/jira/browse/XERCESJ-1130 https://issues.apache.org/jira/browse/XERCESJ-1130

What I did to work around this bug was creating a parent xsd file that included the other xsd files and then set the "schemaResourceResolver" property in the xml configuration with a LSResourceResolver implementation (See http://blog.frankel.ch/xml-validation-with-importedincluded-schemas for example).. 我解决这个问题的方法是创建包含其他xsd文件的父xsd文件,然后使用LSResourceResolver实现在xml配置中设置“schemaResourceResolver”属性(请参阅http://blog.frankel.ch/xml-例如,验证 - 使用importincluded-schemas )..

In your xml configuration add this: 在您的xml配置中添加以下内容:

parent.xsd file looks like this: parent.xsd文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.yourdomain.com/FIXED/EXAMPLE"
           targetNamespace="http://www.yourdomain.com/FIXED/EXAMPLE"
           elementFormDefault="qualified"
           version="1.000"
           id="some_id">
    <xs:include schemaLocation="AccountRequest.xsd"/>
    <xs:include schemaLocation="AccountResponse.xsd"/>
</xs:schema>

In your xml configuration change the schemas property to: 在xml配置中,将schemas属性更改为:

<property name="schemas">
        <list>
        <value>classpath:schema/parent.xsd</value>
        </list>
</property>

Try using MOXy . 尝试使用MOXy You could have a schema mapping defined by annotation, and the other mapping configured in a xml file. 您可以拥有由注释定义的模式映射,以及在xml文件中配置的其他映射。

As far as I know, XStream doesn't provide xml validations, so you could try to do a schema validation before unmarshal. 据我所知,XStream不提供xml验证,因此您可以尝试在解组前进行模式验证。 Using JAXB you could validate required elements/attributes using @XmlElement/@XmlAttribute(required=true) annotation. 使用JAXB,您可以使用@XmlElement/@XmlAttribute(required=true)注释验证所需的元素/属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM