<xml-elements> MoXy?</xml-elements>中的@XmlElementWrapper上的错误

时间:2013-06-05 15:01:46

标签: eclipselink moxy

我发现了一个棘手的错误:

如果我将metadata-xml-binding定义如下

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="package" xml-mapping-metadata-complete="true">
    <xml-schema element-form-default="QUALIFIED" />
    <java-types>
        <java-type name="SearchResult">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="count"/>
                <xml-element java-attribute="requestParameters"/>
                <xml-element java-attribute="pageSize"/>
                <xml-element java-attribute="sortDirection"/>
                <xml-elements java-attribute="results">
                    <xml-element name="GaDictionaryElement" type="it.ga.model.GaDictionary">
                        <xml-element-wrapper name="GaDictionaryElementWrapper" />
                    </xml-element>
                    <xml-element name="OrganizationUnitElement" type="it.ga.model.OrganizationUnit">
                        <xml-element-wrapper name="OrganizationUnitElementWrapper" />
                    </xml-element>
                    <xml-element name="PersonElement" type="it.ga.model.Person">
                        <xml-element-wrapper name="PersonElementWrapper"  />
                    </xml-element>
                    <xml-element name="Empty" type="java.lang.String">
                        <xml-element-wrapper name="EmptyWrapper" nillable="true"/>
                    </xml-element>
                </xml-elements>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

我看到<xml-element-wrapper>标记不起作用,因为它位于<xml-element>标记所包围的<xml-elements>标记中。所以我想找到一种方法以一种干净的方式解决这个问题。因为当我的类的List<?>结果为空时我需要一个空节点。 一个非常糟糕的解决方法是创建许多不同的绑定文件,因为我有List<?>个结果,但我不喜欢它! 此外,是否有人尝试创建一个绑定文件,该文件可用于实现特定接口的类?例如。在某种程度上,我只能看到绑定文件中定义的对象的属性,并指定接口名称的类型?我看到过:[blog]:http://blog.bdoughan.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html#comment-form

3 个答案:

答案 0 :(得分:2)

根据MOXy的处理方式,<xml-element-wrapper><xml-element> <xml-elements>不允许<xml-element-wrapper。您可以在<xml-elements>内添加一个<?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum16943280" xml-mapping-metadata-complete="true"> <java-types> <java-type name="SearchResult"> <xml-root-element/> <java-attributes> <xml-elements java-attribute="results"> <xml-element name="GaDictionaryElement" type="forum16943280.GaDictionary"/> <xml-element name="OrganizationUnitElement" type="forum16943280.OrganizationUnit"/> <xml-element-wrapper name="results"/> </xml-elements> </java-attributes> </java-type> </java-types> </xml-bindings>

<强> oxm.xml

package forum16943280;

import java.util.List;

public class SearchResult {

    private List<Object> results;

    public List<Object> getResults() {
        return results;
    }

    public void setResults(List<Object> results) {
        this.results = results;
    }

}

<强>信息搜索结果

package forum16943280;

public class GaDictionary {

}

<强> GaDictionary

package forum16943280;

public class OrganizationUnit {

}

<强> OrganizationUnit

package forum16943280;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String , Object>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16943280/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {SearchResult.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum16943280/input.xml");
        SearchResult result = (SearchResult) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(result, System.out);
    }

}

<强>演示

<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
   <results>
      <OrganizationUnitElement/>
      <OrganizationUnitElement/>
   </results>
</searchResult>

<强> input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
   <results>
      <GaDictionaryElement/>
      <GaDictionaryElement/>
   </results>
</searchResult>

<强> input.xml中/输出

{{1}}

答案 1 :(得分:1)

如果你想要单独的元素包装器,你可以按类型分割results属性。

<强>信息搜索结果

package forum16943280;

import java.util.List;

public class SearchResult {

    private List<GaDictionary> gaDictionaryResults;
    private List<OrganizationUnit> organizationUnitResults;

    public List<GaDictionary> getGaDictionaryResults() {
        return gaDictionaryResults;
    }

    public void setGaDictionaryResults(List<GaDictionary> results) {
        this.gaDictionaryResults = results;
    }

    public List<OrganizationUnit> getOrganizationUnitResults() {
        return organizationUnitResults;
    }

    public void setOrganizationUnitResults(
            List<OrganizationUnit> organizationUnitResults) {
        this.organizationUnitResults = organizationUnitResults;
    }

}

<强> oxm.xml

<?xml version="1.0"?>
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum16943280" 
    xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="SearchResult">
            <xml-root-element/>
            <java-attributes>
                <xml-element 
                    java-attribute="gaDictionaryResults"
                    name="GaDictionaryElement">
                    <xml-element-wrapper name="GaDictionaryElementWrapper"/>
                </xml-element>
                <xml-element 
                    java-attribute="organizationUnitResults"
                    name="OrganizationUnitElement">
                    <xml-element-wrapper name="OrganizationUnitElementWrapper"/>
                </xml-element>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

<强>演示

package forum16943280;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String , Object>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16943280/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {SearchResult.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum16943280/input.xml");
        SearchResult result = (SearchResult) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(result, System.out);
    }

}

<强> input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
   <GaDictionaryElementWrapper>
      <GaDictionaryElement/>
      <GaDictionaryElement/>
   </GaDictionaryElementWrapper>
</searchResult>

<强> input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<searchResult>
   <OrganizationUnitElementWrapper>
      <OrganizationUnitElement/>
      <OrganizationUnitElement/>
   </OrganizationUnitElementWrapper>
</searchResult>

答案 2 :(得分:0)

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="it.core.widget.dto" xml-mapping-metadata-complete="true">
    <xml-schema element-form-default="QUALIFIED" />
    <java-types>
        <java-type name="JaxbSearchResult" xml-accessor-type="NONE">
            <xml-root-element />
            <xml-type prop-order="count pageSize requestParameters sortDirection results" />
            <java-attributes>
                <xml-element java-attribute="count" />
                <xml-element java-attribute="requestParameters" />
                <xml-element java-attribute="pageSize" />
                <xml-element java-attribute="sortDirection" />
                <xml-elements java-attribute="results">
                    <xml-element name="gaDictionaryElement" type="it.ga.model.GaDictionary" />
                    <xml-element name="organizationUnitElement" type="it.ga.model.OrganizationUnit" />
                    <xml-element name="personElement" type="it.ga.model.Person" />
                    <xml-element-wrapper />
                </xml-elements>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

这是xml,如果我删除<xml-element-wrapper />

,则有效

此外,如果没有该标签,则会产生上述输出

<?xml version="1.0" encoding="UTF-8"?>
<jaxbSearchResult>
    <count>3</count>
    <pageSize>30</pageSize>
    <style>1</style>
    <discriminator>equipment</discriminator>
    <posting>1</posting>
    <CLEAR>1</CLEAR>
    <sortDirection> ASC </sortDirection>
    <gaDictionaryElement>
        <id>24964</id>
        <startDate>2013-03-12T00:00:00</startDate>
        <gaDictionaryMap />
        <booleanMap />
        <integerMap />
        <numberMap />
        <clobMap />
        <parentGaDictionaryLinkSet />
        <childGaDictionaryLinkSet />
        <displayValue>Equipment 2</displayValue>
    </gaDictionaryElement>
    <gaDictionaryElement>
        <id>24962</id>
        <startDate>2013-03-12T00:00:00</startDate>
        <gaDictionaryMap />
        <booleanMap />
        <integerMap />
        <numberMap />
        <clobMap />
        <parentGaDictionaryLinkSet />
        <childGaDictionaryLinkSet />
        <displayValue>Equipment 1</displayValue>
    </gaDictionaryElement>
    <gaDictionaryElement>
        <id>25185</id>
        <startDate>2013-04-22T00:00:00</startDate>
        <gaDictionaryMap />
        <booleanMap />
        <integerMap />
        <numberMap />
        <clobMap />
        <parentGaDictionaryLinkSet />
        <childGaDictionaryLinkSet />
        <displayValue>Attrezzatura test</displayValue>
    </gaDictionaryElement>
</jaxbSearchResult>

我需要相应地将gaDictionaryElement包装为已编组的类型。 任何帮助表示赞赏。 此外,是否有人尝试创建一个绑定文件,该文件可用于实现特定接口的类?例如。在某种程度上,我只能看到绑定文件中定义的对象的属性,并指定接口名称的类型?我见过这个: [博客]:http://blog.bdoughan.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html#comment-form