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

我有一个包含hashmap的简单类:

@XmlRootElement() public class Customer { private long id; private String name; private Map attributes; public Map getAttributes() { return attributes; } public void setAttributes(Map 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 attributes = new HashMap(); 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:

     a1 v1   Jane Doe  

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

     a1 v1   Jane Doe  

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

有关在java.uti.Map中使用XmlAdapter的示例,请参阅:

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


FYI

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

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

上面的注释将对应于以下XML:

   613-555-1111   

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

高级请求