Re: [Simple-support] Interpret a sub-element as String
Brought to you by:
niallg
|
From: Kiran R. <tec...@gm...> - 2015-02-13 10:39:29
|
@Niall,
Appreciate the time you are taking to support this (and also thanks for all
the hard work in creating this wonderful library)!
I did give a try to using Converters, however I could not figure out the
exact way to use them. I have gone through the examples but it is still not
clear to me.
For my use case, I expect that the Converter#read method gives me a
partially parsed object - i.e, I have a RootElement object with the
description field already set. All I have to do is extract the sub-element
substring from the input string and set it to the subElement field.
Similarly, during serialization, I expect that the StringBuilder contains
the description serialized. I only need to fill in the sub-element String.
However, I found out that this is not how the Converter works. It looks
like if I intercept the parsing, I need to perform _all_ parsing manually.
Is this understanding correct? How do I deal with finding child elements in
this case?
On Fri Feb 13 2015 at 11:16:17 AM Niall Gallagher - Yieldbroker <
Nia...@yi...> wrote:
> I was intending to add an @Literal annotation, however I have not yet
> done it. For now you will have to use a Converter, which can be used to
> intercept the XML parsing and compose your own result.
>
>
>
> *From:* Kiran Rao [mailto:tec...@gm...]
> *Sent:* 13 February 2015 15:25
> *To:* Niall Gallagher; simple-support
>
>
> *Subject:* Re: [Simple-support] Interpret a sub-element as String
>
>
>
> 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
>
|