Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top