문제

I have a simple POJO that contains a name/value pair:

public class Parameter {

    private String name = null;
    private String value = null;

    public Parameter() {
        // Do Nothing
    }

    public Parameter(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

I want to marshall this into an XML structure that looks like this:

<parameter name="P3">Parameter 3</parameter>

I've tried a mapping that looks like this but it's not working:

<class name="pkg.Parameter">
    <field name="Name">
        <bind-xml name="name" node="attribute" />
    </field>
    <field name="Value">
        <bind-xml name="paramValue"/>
    </field>
</class>

It gives me this:

<parameter name="P3">
  <paramValue>Parameter 3</paramValue>
</parameter>

That's almost there but the value is obviously misplaced. Essentially I want the value returned from Parameter.getValue() to form the content of the <parameter> element rather than a subelement.

This should be simple, and I'm sure it will be, but I can't seem to get there.

Any ideas?

도움이 되었습니까?

해결책

Oh good grief. It's as if taking the time to explain the problem reorganises everthing in your brain and the solution comes to you just after clicking "Post"!

It turns out that all I needed to do was change

<field name="Value">
    <bind-xml name="paramValue"/>
</field>

to

<field name="Value">
    <bind-xml node="text"/>
</field>

Having done that I'm now seeing this:

<parameter name="P3">Parameter 3</parameter>

Steve

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top