java.util.Map属性的Jaxb名称空间 [英] Jaxb namespaces for java.util.Map properties
问题描述
我有一个包含hashmap的简单类:
I have a simple class that contains a 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());
}
}
主要方法生成以下XML:
The Main method produces the following 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:
The problem I have is that the namespace is dropped when outputting the hashmap. What I would like to generate is xml like this:
<?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>
推荐答案
你可以使用 XmlAdapter
与您一起 java.util.Map
属性获取您要查找的命名空间资格。
You could use an XmlAdapter
with you java.util.Map
property to get the namespace qualification you are looking for.
有关使用 XmlAdapter
并使用 java.uti.Map
的示例,请参阅:
For an example of using XmlAdapter
with java.uti.Map
see:
- http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
更多信息在JAXB和名称空间上:
For more info on JAXB and namespaces :
- http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html
FYI
我正在考虑添加分机 EclipseLink JAXB(MOXy)以更好地处理这种情况:
I am considering adding an extension to EclipseLink JAXB (MOXy) to better handle this scenario:
@XmlMap(wrapper="my-entry", key="@my-key", value="my-value")
public Map<String, PhoneNumber> phoneNumbers = new HashMap<String, PhoneNumber>();
上述注释将对应于以下XML:
The above annotation would correspond to the following XML:
<phoneNumbers>
<my-entry my-key="work">
<my-value>613-555-1111</value>
</my-entry>
</phoneNumbers>
键/值属性将是XPath语句,命名空间信息将遵循为其他语义执行的操作MOXy扩展(例如见下面的链接):
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):
- http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
增强请求
- https://bugs.eclipse.org/322423
这篇关于java.util.Map属性的Jaxb名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!