Re: [Simple-support] Interpret a sub-element as String
Brought to you by:
niallg
|
From: Kiran R. <tec...@gm...> - 2015-02-13 04:25:18
|
Hi Niall,
My requirement is after parsing, subElement should contain the string that
represents sub-element in its entirety. i.e.,
assert subElement == "<sub-element id=\"1\"><name>Hello
World</name></sub-element>";
I admit this is not a typical use case.
@Path("sub-element")
@Element(name="name")
public String name;
This succeeds - but then this is not what I want.
@Path("sub-element")
@Element
public String name;
This results in a ValueRequiredException.
On Fri Feb 13 2015 at 8:37:29 AM Niall Gallagher <gal...@ya...>
wrote:
> Did you try
>
> @Path("sub-element")
> @Element(name="name")
> public String subElement;
>
> or
>
> @Path("sub-element")
> @Element
> public String name;
>
> --------------------------------------------
> On Thu, 12/2/15, Kiran Rao <tec...@gm...> wrote:
>
> Subject: Re: [Simple-support] Interpret a sub-element as String
> To: "simple-support" <sim...@li...>
> Received: Thursday, 12 February, 2015, 8:31 PM
>
> 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:
> @Rootpublic 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.
>
>
>
>
>
>
>
>
>
>
>
>
> -----Inline Attachment Follows-----
>
> ------------------------------------------------------------
> ------------------
> Dive into the World of Parallel Programming.
> The Go Parallel Website,
> sponsored by Intel
> and developed in partnership with Slashdot Media, is your
> hub for all things parallel software
> development, from weekly thought
> leadership
> blogs to news, videos, case studies, tutorials and more.
> Take a
> look and join the conversation now.
> http://goparallel.sourceforge.net/
> -----Inline Attachment Follows-----
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
|