mapping XML collection to POJOs collection with different structrure

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

mapping XML collection to POJOs collection with different structrure

Hello everyone,

I am trying to convert large XML file containing collection of objects into a list of POJOs and having difficulties with converting one structure into another.

I'll describe very simplified example below.

Let's suppose we have input XML file like this:
<?xml version="1.0" encoding="UTF-8"?>

What I want to do is to convert it into a collection of Java objects like this:
package model;

public class SubItem {
    private String name = "";
    private String t1 = "";
    private String t2 = "";

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getT1() {
        return t1;

    public void setT1(String t1) {
        this.t1 = t1;

    public String getT2() {
        return t2;

    public void setT2(String t2) {
        this.t2 = t2;

    public String toString() {
        return "SubItem{" + "name=" + name + ", t1=" + t1 + ", t2=" + t2 + '}';

I.e. each <subItem/> shall be represented by one SubItem instance, and this subitem instance has to have properties set from the tags above this particular <subItem/> tag.

Expected result should be
SubItem{name=SI-1, t1=T1-A, t2=T2-F}
SubItem{name=SI-2, t1=T1-A, t2=T2-F}
SubItem{name=SI-3, t1=, t2=T2-J}
SubItem{name=SI-4, t1=, t2=T2-J}

I am trying to use following Smooks configuration XML:
<?xml version="1.0"?>
<smooks-resource-list xmlns="" 
    <jb:bean beanId="subItemList" class="java.util.ArrayList" createOnElement="/items">
        <jb:wiring beanIdRef="subItem" />
    <jb:bean beanId="subItem" class="model.SubItem" createOnElement="subItem" retain="true">
        <jb:wiring property="t1" beanIdRef="t1"/>
        <jb:wiring property="t2" beanIdRef="t2"/>
        <jb:value property="name" decoder="String" data="subItem" />
        <jb:value property="t1" decoder="String" data="../../subItems/tag1" />
    <jb:value beanId="t1" data="item/subItems/tag1" default="null"/>
    <jb:value beanId="t2" data="item/subItems/tag2" default="null"/>

However what I actually get is:
SubItem{name=SI-1, t1=T1-A, t2=}
SubItem{name=SI-2, t1=T1-A, t2=T2-J}
SubItem{name=SI-3, t1=T1-A, t2=}
SubItem{name=SI-4, t1=T1-A, t2=}

It looks like Smooks cannot access future events from the stream and I cannot guarantee input XML file will have it's tags properly organized.
Setting missing tags data from previous <item/> is also something I'd like to avoid.

Documentation mentions mixing SAX and DOM models that can theoretically be helpful but is seems xmlns:jb doesn't support this at all.

It's also seems possible to make conversion in two steps, first convert input XML into XML with appropriate structure and then convert XML to POJOs but it seems this approach will have a little bit of redundancy in it.

Please suggest if this conversion like this is at all possible and if yes then how do I implement it?

Thanks in advance,

P.S. I've uploaded whole sample project on Dropbox just in case