Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top