Empty Entry In Elementlist Simplexml
my question is simple, but I can't find nothing about it. I Have a list class and an entry class for XML Serialization: @Root(name = 'entries') public class List { @ElementLis
Solution 1:
You can check for empty elements manually:
@Root(name = "entries")@Convert(List.ListConverter.class)// Set the converterpublicclassList
{
@ElementList(required = false, entry = "entry", inline = true, empty = true)private java.util.List<Entry> entries;
publicvoidadd(Entry e)
{
// Just for testing
entries.add(e);
}
staticclassListConverterimplementsConverter<List>
{
@Overridepublic List read(InputNode node)throws Exception
{
Listl=newList();
InputNodechild= node.getNext("entry");
while( child != null)
{
if( child.isEmpty() == true ) // child is an empty tag
{
// Do something if entry is empty
}
else// child is not empty
{
Entrye=newPersister().read(Entry.class, child); // Let the Serializer read the Object
l.add(e);
}
child = node.getNext("entry");
}
return l;
}
@Overridepublicvoidwrite(OutputNode node, List value)throws Exception
{
// Not required for reading ...thrownewUnsupportedOperationException("Not supported yet.");
}
}
}
How to use:
Serializerser=newPersister(newAnnotationStrategy()); // Set AnnotationStrategy here!Listl= ser.read(List.class, yourSourceHere);
Documentation:
Solution 2:
To avoid the error in parse do one should place annotation tags @set e @get
@Root(name = "entries", strict = false)
public class List {
@set:ElementList(required = false, entry = "entry", inline = true, empty = true)
@get:ElementList(required = false, entry = "entry", inline = true, empty = true)
private List<Entry> entries;
}
@Root
public class Entry {
@set:Element(name = "entry_id", required = true)
@get:Element(name = "entry_id", required = true)
private long id;
@set:Element(name = "text", required = true)
@get:Element(name = "text", required = true)
private String Text;
}
Post a Comment for "Empty Entry In Elementlist Simplexml"