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

java.util.Map属性的Jaxb名称空间,java,jaxb,jaxb2,Java,Jaxb,Jaxb2,我有一个包含hashmap的简单类: @XmlRootElement() public class Customer { private long id; private String name; private Map<String, String> attributes; public Map<String, String> getAttributes() { return attributes; }

我有一个包含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());

    }

}
@XmlRootElement()
公共类客户{
私人长id;
私有字符串名称;
私有地图属性;
公共映射getAttributes(){
返回属性;
}
公共void集合属性(映射属性){
this.attributes=属性;
}
@XmlAttribute
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共静态void main(字符串[]args)引发异常{
jaxbcontextjc=
newInstance(“com.rbccm.dbos.payments.dao.test”);
客户=新客户();
customer.setId(123);
customer.setName(“Jane Doe”);
HashMap attributes=新的HashMap();
属性。put(“a1”、“v1”);
customer.setAttributes(属性);
StringWriter sw=新的StringWriter();
Marshaller m=jc.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式化的_输出,true);
m、 元帅(客户、软件);
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>
<phoneNumbers>
    <my-entry my-key="work">
        <my-value>613-555-1111</value>
    </my-entry>
</phoneNumbers>

a1
v1
无名氏
我遇到的问题是,在输出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>

a1
v1
无名氏
您可以使用with-You
java.util.Map
属性来获取您要查找的名称空间限定

有关将
XmlAdapter
java.uti.Map
一起使用的示例,请参阅:

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


FYI

我正在考虑添加扩展以更好地处理此场景:

@XmlMap(wrapper="my-entry", key="@my-key", value="my-value")
public Map<String, PhoneNumber> phoneNumbers = new HashMap<String, PhoneNumber>(); 
@XmlMap(wrapper=“my entry”,key=“@my key”,value=“my value”)
public-Map-phoneNumbers=new-HashMap();
上述注释将对应于以下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>
<phoneNumbers>
    <my-entry my-key="work">
        <my-value>613-555-1111</value>
    </my-entry>
</phoneNumbers>

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

增强请求


是的,这很有效。但是,对于要在hashmap中用作值的每种类型,似乎必须创建3个额外的类。有了这种开销,在映射元素上没有名称空间可能更可取。(另一个链接非常有用,您可能已经注意到我的问题99%基于该示例)该扩展看起来非常有用。schemagen任务也可以使用它吗?@Heathern-当我们添加它时,它肯定可以使用
JAXBContext.generateSchema()
方法()。我们有另一个增强请求,用于添加用于模式生成()的ant任务。