java.util.Map属性的Jaxb名称空间
我有一个包含hashmap的简单类: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; }
@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-Youjava.util.Map
属性来获取您要查找的名称空间限定
有关将XmlAdapter
与java.uti.Map
一起使用的示例,请参阅:
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扩展所做的操作(例如,请参见下面的链接):
JAXBContext.generateSchema()
方法()。我们有另一个增强请求,用于添加用于模式生成()的ant任务。