Re: [Simple-support] Deserializing specific (lists of) children without having to map an entire XML
Brought to you by:
niallg
|
From: H. B. <he...@zx...> - 2011-01-20 11:14:17
|
Niall, thanks for the prompt reply. I've been playing with it a bit,
and I found out in this particular example I can skip the Section part
altogether, as it is a List that is a member of SearchSuggestion.
Right now I have the following classes:
@Root(name="SearchSuggestion")
private static class Result {
@Element
String Query;
@ElementList
private ArrayList<Item> Section;
public List<Item> getSection(){
return Section;
}
}
@Root(strict=false)
private static class Item {
@Element(name="Text")
String text;
@Element(name="Description")
String description;
@Element(name="Url")
String url;
}
And am getting the payload with:
Result result = serializer.read(Result.class, input, false);
...and getting the Items with List<Item> list = result.getSection();
So I still have 2 classes instead of one, is this the absolute
minimum? I noticed I can't do List<Item> list =
serializer.read(Item.class, input, false) (possibly with a different
input source, e.g. a node) and have the serializer return the list in
one go. Is there any way, for instance, that I can grab a Node
(org.w3c.*) through (java.xml.xpath) Xpath and feed it into the
Persister as an InputNode? That way I can first select the nearest
element to Item and work with that (while typing this I'm realizing I
might be able to let XPath compile a list of nodes that contain just
<Item></Item> and work with that...).
Another question that is somewhat related, an Item in this case has an
<Image source="..."> element, I figured I could get it by adding a
@Path("Image/@source") to a String in the Item class, but this
consistently gets me a null. I was however able to get the attribute
by defining an Image class with an @Attribute String source. This is
basically the same question as before, apart from the class being on
the other side of the hierarchy, but is there any way I can reach the
attribute from inside Item without having to define an Image class?
|