JAXB unmarshaling XmlJavaTypeAdapter

时间:2012-03-28 09:36:04

标签: jaxb

如何使用JAXB注释(XMLJavaTYpeAdapter)描述Java类?实际上我想将以下XML格式解组为Java对象。

<map>
    <entry key="extraProperties">
        <map>
            <entry key="message" value=""/>
            <entry key="entity">
                <map>
                    <entry key="starttime" value="1330086091375"/>
                    <entry key="state" value="1"/>
                    <entry key="uptime" value="2837656746"/>
                </map>
            </entry>
            <entry key="childResources">
                <map>
                    <entry key="transaction-service" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/transaction-service"/>
                    <entry key="http-service" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/http-service"/>
                    <entry key="applications" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/applications"/>
                    <entry key="deployment" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/deployment"/>
                    <entry key="jvm" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/jvm"/>
                    <entry key="web" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/web"/>
                    <entry key="security" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/security"/>
                    <entry key="network" value="https://47.168.96.31:4848/monitoring/domain1/instanceMaster/network"/>
                </map>
            </entry>
        </map>
    </entry>
    <entry key="message" value=""/>
    <entry key="exit_code" value="SUCCESS"/>
    <entry key="command" value="Monitoring Data"/>
</map>

1 个答案:

答案 0 :(得分:0)

我已经定义了两个类名Map和Entry,它将Map元素保存为List,另外还有两个属性。好了,因为这个xml结构是静态的,所以创建类对应于XML是很好的。我想知道以下xml如何将结构映射到java classess。 正如您所看到的,某些条目标签具有列表元素,某些条目标签onty具有地图元素,它基于rntry密钥名称。

<map>
    <entry key="extraProperties">
        <map>
            <entry key="methods">
                <list>
                    <map></map>
                    <map></map>
                </list>
            </entry>
            <entry key="instanceList">
                <list>
                    <map></map>
                    <map></map>
                </list>
            </entry>
        </map>
    </entry>
    <entry key="message" value="instanceMaster   running  
instanceRemote   running  "/>
    <entry key="exit_code" value="SUCCESS"/>
    <entry key="command" value="list-instances AdminCommand"/>
</map>