1

我有一个生成 jaxb 类的 xsd。为此,我为通用地图创建了一个 XML 适配器。该适配器通过了单元测试,并且生成的变量的类型适当地是预期的映射。但问题是,当尝试验证从这种情况下创建的对象时,我得到“无法将 'hashMap' 类型解析为元素 'autoExecuteArguments' 的定义。我已经摆弄了很多,但总是以上述的一些变化结束错误。这两个绑定没有一起使用,但举例说明了试图达到某种解决方案。

      <xjc:javaType name="java.util.Map" xmlType="mp:MapTypeEntry"
        adapter="mil.dod.th.ose.core.impl.mp.UtilMapConverter" />

 <jaxb:bindings schemaLocation="resources/missionProgramSchema/MissionProgram.xsd">
    <jaxb:bindings node="//xs:element[@name='MissionProgramInstance']">
        <jaxb:bindings node="//xs:element[@name='autoExecuteArguments']">
            <jaxb:property>
                <jaxb:baseType name="java.util.HashMap">
                    <xjc:javaType name="java.util.Map"
                    adapter="//core.impl.mp.UtilMapConverter" />
                </jaxb:baseType>
            </jaxb:property>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings> 

我相信最后一个是对绑定的嘲弄,因此我无法通过大量的 tweeking 使其工作,我得到 '[ERROR] 编译器无法兑现此属性自定义。它附加到错误的位置,或者与其他绑定不一致。

接下来是转换器:

@XmlSeeAlso(UtilMapConverter.MapType.class)
public class UtilMapConverter<K, V> extends XmlAdapter<UtilMapConverter.MapType<K,V>, Map<K, V>> {

@Override
public UtilMapConverter.MapType<K, V> marshal(final Map<K, V> map) 
{
    final MapType<K, V> mapType = new MapType<K, V>();
    for (Map.Entry<K, V> entry : map.entrySet()) 
    {
        final MapTypeEntry<K, V> mapEntryType = new MapTypeEntry<K, V>(); 
        mapEntryType.setKey(entry.getKey());
        mapEntryType.setValue(entry.getValue());
        mapType.getEntry().add(mapEntryType);
    }
    return mapType;
}

@Override
public Map<K, V> unmarshal(final UtilMapConverter.MapType<K, V> genericMap) 
{
    final Map<K, V> map = new HashMap<K, V>();

    for (MapTypeEntry<K, V> mapEntryType : genericMap.getEntry()) 
    {
        map.put(mapEntryType.getKey(), mapEntryType.getValue());
    }
    return map;
}

@XmlAccessorType(XmlAccessType.PROPERTY)
public static class MapType<K, V> 
{
    private List<MapTypeEntry<K, V>> m_Entry = new ArrayList<MapTypeEntry<K, V>>();

    public MapType()
    {  
    }

    public MapType(final Map<K, V> map) 
    {
        for (Map.Entry<K, V> e : map.entrySet()) 
        {
            m_Entry.add(new MapTypeEntry<K, V>(e));
        }
    }
    @XmlElement
    @XmlSchemaType(name = "mapType")
    public List<MapTypeEntry<K, V>> getEntry() 
    {
        return m_Entry;
    }
    public void setEntry(final List<MapTypeEntry<K, V>> entry) 
    {
        this.m_Entry = entry;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public static class MapTypeEntry<K, V> 
{

    @XmlElement(required = true)
    @XmlSchemaType(name = "key")
    protected K m_Key;


    @XmlElement(required = true)
    @XmlSchemaType(name = "value")
    protected V m_Value;


    public MapTypeEntry() 
    {
    }


    public MapTypeEntry(final Map.Entry<K, V> entry) 
    {
        m_Key = entry.getKey();
        m_Value = entry.getValue();
    }


    @XmlElement
    public K getKey() 
    {
        return m_Key;
    }

    @XmlElement
    public V getValue() 
    {
        return m_Value;
    }
}

我知道我有一个烂摊子。我只是想弄清楚什么是有帮助的,什么是伤害的,什么是愚蠢的。最后是xsd:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://core/mp/model" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://core/mp/model"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" elementFormDefault="qualified"
id="MissionProgramInstance" jaxb:version="2.1"
jaxb:extensionBindingPrefixes="xjc">
<xs:include schemaLocation="MissionProgram.xsd" />
<xs:element name="MissionProgramInstance">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="program" type="MissionProgram"
                minOccurs="1" />
            <xs:element name="autoExecuteArguments" minOccurs="0">
                <xs:annotation>
                    <xs:appinfo>
                        <jaxb:property>
                            <jaxb:baseType name="java.util.Map" />
                        </jaxb:property>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="autoExecuteEnabled" type="xs:boolean"
            use="required" />
        <xs:attribute name="assetDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="physicalLinkDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="linkLayerDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="transportLayerDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="imageStreamDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="programDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>
<xs:complexType name="MissionProgram">
    <xs:sequence>
        <!--This field holds source data for a mission program -->
        <xs:element name="source" type="xs:string" minOccurs="1" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="MapType">
    <xs:sequence>
        <xs:element name="autoExecuteArgument" type="MapTypeEntry">
            <xs:annotation>
                <xs:appinfo>
                    <jaxb:property>
                        <jaxb:baseType name="//core.mp.impl.UtilMapConverter.MapType" />
                    </jaxb:property>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
</xs:complexType>
<xs:simpleType name="MapTypeEntry">
    <xs:list itemType="xs:anySimpleType" />
</xs:simpleType>
    </xs:schema>

当前的演示文稿中存在一些冗余。我是 xsd 和 jaxb 的新手。我试图弄明白这一切,但尽管试图研究,我认为我找错了地方,因为我似乎找到了碎片,但没有对所有这些一起工作的解释。这是我尝试将至少部分完全放在一起的主要资源之一:http: //blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html。还有用于 xml 适配器的 javadocs,以及到处浏览的内容。谢谢你,如果你花时间至少思考一下“你会做什么?”

4

1 回答 1

0

我会做(并且已经做过)是编写一个干净的 xsd 并使用 xjc 生成 java 类。如果您需要更“编码器友好”的数据结构,请编写您自己的代码来在反序列化后转换 jaxb 对象。试图强制 xml 适应你认为你希望你的数据结构看起来的方式通常是一种沮丧的练习,并且随着你的项目随着时间的推移而改变,这会让你的生活变得困难。

于 2012-08-12T20:52:13.543 回答