For those of you out there who have problem implementing some complex xmls i have a good example for you:
Xml :
[code]
UIElement
Elements DivContainer name="asdf" Div name="Carthago1" test Teszt1 test Div Div name="Carthago2" test Teszt2 test Div DivContainer DivContainer name="asdf2" Div name="Carthago2" test Teszt2 test Div Div name="Carthago2" test Teszt3 test
@Root
public class DivContainer
{
@ElementList(name="DivContainer", inline=true)
public List
divs;
@Attribute
public String name;
}
@Root(name="Div")
public class Div
{
@Element(name="test")
public String test;
@Attribute(name="name", required=false)
public String name;
}
[/code]
And the serialize code:
[code]
public void testXml()
{
Serializer serializer = new Persister();
File source = new File("c:\\tmp\\test.xml");
UIElement ui = null;
try
{
ui = serializer.read(UIElement.class, source);
} catch (Exception e)
{
e.printStackTrace();
}
for (DivContainer divc : ui.divContainers)
{
for (Div div : divc.divs)
{
System.out.println("Value of test: " + div.test);
}
}
}
[/code]
You have to look out for two things... First note the @Root under Div tag. That is necessary there. Writing in the attribute name there is necessary too. Also the 'inline' tag. Without them it wont work.
Again sorry for the indentation problem... Blogspot is lame in detecting code.
Gergely.
Nincsenek megjegyzések:
Megjegyzés küldése