简体   繁体   English

java.util.Map 属性的 Jaxb 命名空间

[英]Jaxb namespaces for java.util.Map properties

I have a simple class that contains a hashmap:我有一个简单的 class 包含一个 hashmap:

@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());

    }

}

The Main method produces the following XML: 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>

The problem I have is that the namespace is dropped when outputting the hashmap.我遇到的问题是在输出 hashmap 时删除了命名空间。 What I would like to generate is xml like this:我想生成的是 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>

You could use an XmlAdapter with you java.util.Map property to get the namespace qualification you are looking for.您可以将XmlAdapterjava.util.Map属性一起使用来获得您正在寻找的命名空间资格。

For an example of using XmlAdapter with java.uti.Map see:有关将XmlAdapterjava.uti.Map一起使用的示例,请参见:

For more info on JAXB and namespaces:有关 JAXB 和命名空间的更多信息:


FYI供参考

I am considering adding an extension to EclipseLink JAXB (MOXy) to better handle this scenario:我正在考虑向EclipseLink JAXB (MOXy)添加扩展以更好地处理这种情况:

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

The above annotation would correspond to the following XML:上述注释将对应于以下 XML:

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

The key/value properties would be XPath statements, and the namespace information would follow what is done for other MOXy extensions (for an example see link below):键/值属性将是 XPath 语句,命名空间信息将遵循其他 MOXy 扩展所做的操作(有关示例,请参见下面的链接):

Enhancement Request高级请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM