Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the XML Simple Framework to deserialize a list of elements containing CDATA

Given the following XML:

<stuff>
    <item id="1"><![CDATA[first stuff...]]></item>
    <item id="2"><![CDATA[more stuff...]]></item>
</stuff>

I am struggling mightily to figure out how to deserialize this with the Simple Framework. I have started out with the following Java classes:

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Root;
import org.simpleframework.xml.ElementList;

@Root(name="stuff")
public class Stuff {

    @ElementList(inline=true)
    public List<Item> itemList = new ArrayList<Item>();
}

and

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

@Element(name="item", data=true)
public class Item {

    @Attribute
    public String id;
}

So the missing piece for me is how do I access the CDATA content for each item element?

like image 911
pajato0 Avatar asked Oct 24 '25 14:10

pajato0


1 Answers

I patiently waited for my son to write up the solution he suggested which turned out to solve the problem. Evidently he will have nothing to do with an organization that would have me as a member, to only slightly distort Groucho's eternal mantra. Here is his suggestion, provided so that other's looking to solve this puzzle have a handy solution:

Modify the Item class as follows:

import org.simpleframework.Attribute;
import org.simpleframework.Text;

public class Item {

    @Attribute
    public String id;

    @Text(data=true)
    public String value;
}

so that the field value will contain the CDATA text.

like image 50
pajato0 Avatar answered Oct 27 '25 05:10

pajato0