Re: [Simple-support] Interpret a sub-element as String
Brought to you by:
niallg
|
From: Kiran R. <tec...@gm...> - 2015-02-12 09:31:21
|
Tried the @Path annotation as well. The problem I found there is that
ultimately, I need to also annotate the field that has the Path annotation
with another annotation that states whether it is an element, attribute or
Text.
Since my aim is to extract the value as a String, I tried the @Text
annotation:
@Root
public class RootElement {
@Element
public String description;
@Path("sub-element")
@Text
public String subElement;
public RootElement() {
}
}
I also tried @Element. However none of this works. I get a
ValueRequiredException for subElement. I can of course add a
required=false, but that does not serve my purpose.
It looks like I'm missing some fundamental point about how Path expressions
are supposed to work!
Also, the docs for Path state that a subset of XPath expressions are
supported and goes on to list a few examples. Is this an exhaustive list of
supported expressions?
I tried something like @Path("sub-element/text()") and it threw a
PathException.
On Thu Feb 12 2015 at 6:52:44 AM Niall Gallagher - Yieldbroker <
Nia...@yi...> wrote:
> Take a look at the @Path annotation
>
>
>
> *From:* Kiran Rao [mailto:tec...@gm...]
> *Sent:* 11 February 2015 20:19
> *To:* simple-support
> *Subject:* [Simple-support] Interpret a sub-element as String
>
>
>
> Hi,
>
>
>
> Here's an XML I have:
>
>
>
> <root-element>
>
> <description>lorem-ipsum</description>
>
> <sub-element id="1"><name>Hello World</name></sub-element>
>
> </root-element>
>
>
>
> Now, for whatever reason, I want to read the <sub-element> part as a
> String rather than as an object - i.e.,
>
>
>
> @Root(name="root-element", strict=false)
>
> class RootElement {
>
> @Element(name="description")
>
> String description
>
>
>
> //No @Element annotation here.
>
> String subElement;
>
> }
>
>
>
>
>
> RootElement root = new Persister().read(RootElement.class, INPUT);
>
>
>
> RootElement EXPECTED = new RootElement("lorem-ipsum", "<sub-element
> id=\"1\"><name>Hello World</name></sub-element>");
>
> assertEquals(EXPECTED, root);
>
>
>
> Similarly, when I serialize this RootElement object, I want the string to
> appear as a sub-element.
>
>
>
>
>
> Any idea on how to go about doing this? It looks like I am looking for
> some sort of interceptor - I get the partially parsed object and then I can
> fill in the missing fields.
>
>
>
> I tried using a Converter and the @Convert annotation; I tried
> implementing a Visitor - in both cases I got stuck trying to figure out
> what portion has been parsed already.
>
>
>
> I have temporarily written code to first parse the object with all fields
> except subElement; and then set this field using sub-string operations.
> However, this is not really scalable - especially when I use sub-element
> inside a list, inside other parent elements, or when root-element itself
> has other parent elements etc.
>
>
>
>
>
>
>
|