JAXB马歇尔DynaBean

时间:2019-07-02 08:01:26

标签: java serialization jaxb xmladapter

我想将DynaBean实例编组为xml,但我想将DynaProperties作为属性。我对BasicDynaBean有一个非常简单的扩展名:

@XmlRootElement
private static class DynaBeanExtension extends BasicDynaBean {
    public DynaBeanExtension() {
        super(null);
    }
    public DynaBeanExtension(DynaClass dynaClass) {
        super(dynaClass);
    }
    @XmlElement
    @Override
    public Map<String, Object> getMap() {
        return super.getMap();
    }
}

我创建它的一个实例并尝试编组:

DynaBeanExtension dynaBean = new DynaBeanExtension(dynaClass);
dynaBean.set("firstName", "SampleName");
dynaBean.set("lastName", "SampleSurname");
JAXBContext jaxbContext = JAXBContext.newInstance(DynaBeanExtension.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

我得到的结果如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dynaBeanExtension>
    <map>
        <entry>
            <key>firstName</key>
            <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">SampleName</value>
        </entry>
        <entry>
            <key>lastName</key>
            <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">SampleSurname</value>
        </entry>
    </map>
</dynaBeanExtension>

如您所见,映射是使用默认机制序列化的。 XmlAdapter实现的外观如何,以便firstName和lastName是属性?

0 个答案:

没有答案