ハッシュマップを含む単純なクラスがあります。
@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());
}
}
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>
私が抱えている問題は、ハッシュマップを出力するときに名前空間が削除されることです。私が生成したいのは、次のような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>