3

ハッシュマップを含む単純なクラスがあります。

@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>
4

1 に答える 1

0

XmlAdapterwith youプロパティを使用して、java.util.Map探している名前空間修飾を取得できます。

XmlAdapterwithの使用例については、以下をjava.uti.Map参照してください。

JAXB と名前空間の詳細については、次を参照してください。


ご参考までに

このシナリオをより適切に処理するために、 EclipseLink JAXB (MOXy)に拡張機能を追加することを検討しています。

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

上記の注釈は、次の XML に対応します。

<phoneNumbers>
    <my-entry my-key="work">
        <my-value>613-555-1111</value>
    </my-entry>
</phoneNumbers>

キー/値のプロパティは XPath ステートメントになり、名前空間情報は他の MOXy 拡張機能で行われることに従います (例については、以下のリンクを参照してください)。

機能強化のリクエスト

于 2011-06-06T17:00:28.410 に答える