0

As I am new to JiBX, I don't know if this is possible, but I feel like I am overlooking something. I am trying to find a way to bind two properties from one POJO, to the same structure in two different ways. One as the value of the structure, the second as an attribute of the same structure.

The example below shows what I intend to do.

Keep in mind that this binding will only have to work one way, from Java to XML. (The binding will be defined as <binding direction="output">)

A simplified version of the classes I am trying to map:

public Example(){
    private objectToMap;

    //Constructor, getters and setters go here
}

public ObjectToMap(){
    private String randomProperty;
    private String value = "a";
    private Attribute;

    //Constructor, getters and setters go here
}

public Attribute(){
    private String attribute = "b";

    //Constructor, getters and setters go here
}

The XML I want to generate has too look like this:

<Example>
    <RandomProperty>randomValue</RandomProperty>
    <ObjectToMapValue attribute="b">a</ObjectToMapValue>
</Example>

And this is where I get stuck:

<binding direction="output">
    <mapping name="Example" class="com.example.pojo.Example">
        <structure field="objectToMap" type="com.example.pojo.ObjectToMap">
            <value name="RandomProperty" field="randomProperty">
            <structure name="ObjectToMapValue" field="value"/>
            <!-- But how to add the attribute? -->
        </structure>
    </mapping>
</binding>

Is it possible to make a JiBX binding do this?

1 Answers1

0

Got it working, and as I thought, I was overlooking something. This is what solved it:

<binding direction="output">
    <mapping name="Example" class="com.example.pojo.Example">
        <structure field="objectToMap" type="com.example.pojo.ObjectToMap">
            <value name="RandomProperty" field="randomProperty">
            <structure name="ObjectToMapValue" field="value">
                <structure field="Attribute"/>
                <value style="text" field="value"> 
            </structure>
        </structure>
    </mapping>
    <mapping class="com.example.pojo.Attribute" abstract="true">
        <value name="attribute" field="attribute" style="attribute">
    </mapping>
</binding>