【问题标题】:Jaxb namespaces for java.util.Map propertiesjava.util.Map 属性的 Jaxb 命名空间
【发布时间】:2011-06-06 16:53:09
【问题描述】:

我有一个包含哈希图的简单类:

@XmlRootElement()
public class Customer {

    private long id;
    private String name;

    private Map<String, String> attributes;

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

    @XmlAttribute
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc =
           JAXBContext.newInstance("com.rbccm.dbos.payments.dao.test");

        Customer customer = new Customer();
        customer.setId(123);
        customer.setName("Jane Doe");

        HashMap<String, String> attributes = new HashMap<String, String>();
        attributes.put("a1", "v1");
        customer.setAttributes(attributes);


        StringWriter sw = new StringWriter();
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(customer, sw);
        System.out.println(sw.toString());

    }

}

Main 方法生成以下 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:customer id="123" xmlns:ns2="http://www.example.org/package">
    <ns2:attributes>
        <entry>
            <key>a1</key>
            <value>v1</value>
        </entry>
    </ns2:attributes>
    <ns2:name>Jane Doe</ns2:name>
</ns2:customer>

我遇到的问题是在输出 hashmap 时命名空间被删除了。我想生成的是这样的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:customer id="123" xmlns:ns2="http://www.example.org/package">
    <ns2:attributes>
        <ns2:entry>
            <ns2:key>a1</ns2:key>
            <ns2:value>v1</ns2:value>
        </ns2:entry>
    </ns2:attributes>
    <ns2:name>Jane Doe</ns2:name>
</ns2:customer>

【问题讨论】:

    标签: java jaxb jaxb2


    【解决方案1】:

    您可以使用 XmlAdapterjava.util.Map 属性来获取您正在寻找的命名空间限定条件。

    有关使用 XmlAdapterjava.uti.Map 的示例,请参阅:

    有关 JAXB 和命名空间的更多信息:


    仅供参考

    我正在考虑为EclipseLink JAXB (MOXy) 添加一个扩展以更好地处理这种情况:

    @XmlMap(wrapper="my-entry", key="@my-key", value="my-value")
    public Map<String, PhoneNumber> phoneNumbers = new HashMap<String, PhoneNumber>(); 
    

    上述注解将对应于以下 XML:

    <phoneNumbers>
        <my-entry my-key="work">
            <my-value>613-555-1111</value>
        </my-entry>
    </phoneNumbers>
    

    键/值属性将是 XPath 语句,命名空间信息将遵循其他 MOXy 扩展所做的操作(示例见下面的链接):

    增强请求

    【讨论】:

    • 是的,这行得通。但看起来您必须为要用作哈希图中的值的每种类型创建 3 个额外的类。有了这种开销,在地图元素上没有命名空间可能更可取。 (另一个链接非常有帮助,您可能已经注意到我的问题是基于该示例的 99%)
    • 这个扩展看起来很有用。 schemagen 任务也可以使用它吗?
    • @Heathen - 当我们添加它时,它肯定会使用 JAXBContext.genertateSchema() 方法 (wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema)。我们有另一个关于添加 ant 任务以生成模式的增强请求 (bugs.eclipse.org/336076)。
    猜你喜欢
    • 2012-05-29
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2014-06-07
    • 2010-12-10
    • 1970-01-01
    相关资源
    最近更新 更多