I am using SimpleXML framework for deserializing backend answers. I made some assumptions about elements. Some elements do not meet these requirements. For example, I expect an element to have childs <ID>
and <face>
. If my user is not allowed to see a specific element, I might get an answer like this:
<list>
<voucher type="hiddenobject">
<face>foo</face>
</voucher>
<voucher type="object">
<ID>42</ID>
<face>bar</face>
</voucher>
</list>
Which gives me a ValueRequiredException for the following deserialization class:
@Root
class Voucher {
@Element(name="ID")
private String id;
@Element
private String face;
}
I would like to ignore these objects with type hiddenobject
. I learned about the VisitorStrategy
and implemented a simple Visitor
like so:
private static final class HiddenObjectVisitor implements Visitor {
@Override
public void read(Type type, NodeMap<InputNode> node) throws Exception {
String nodeType = node.getNode().getAttribute("type").getValue();
if (nodeType != null && nodeType.equals("hiddenobject")) {
Log.d(TAG, "skipping node " + node);
node.getNode().skip();
}
}
@Override
public void write(Type type, NodeMap<OutputNode> node) throws Exception {
// stub
}
}
and added this Visitor
to a VisitorStrategy
VisitorStrategy strategy = new VisitorStrategy(new HiddenObjectVisitor());
expecting that this would skip nodes during deserialization. I do get log entries stating that the node would be skipped. Anyway, the VisitorStrategy
seems to keep parsing the node to be skipped, resulting in a ValueRequiredException
.
How can I ignore nodes having a given attribute? Is it possible to use VisitorStrategy
for this task?