<div><strong>Formatted Text</strong>unformatted text</div>
I've tried the suggested technique of mapping the object:
@RootElement("div")
class Root{
@Element(name="strong", targetType=String.class)
@Text
List<String> elements;
}
But all I get back is a list with 2 null elements.
Can anyone spot any obvious mistakes/suggest an alternative
implementation?
Thanks
@RootElement("Document")
public class XmapprKmlDocument {
@Element("Placemark")
private List<XmapprKmlPlacemark> placemarks;
placemarks seems to be null when parsed.
So my strong class would be:
@RootElement
class Strong
{
@Text
String text;
}
I'll have a look at my document class again. The list placemarks had
getters and setters so I might have missed something.
Thanks for replying!
Richard
I've been putting root element annotation on all my classes! I thought
you had to annotate the root element (ie the highest level) for each
class. In other words I thought the root element was relative to the
class not the the whole xml document!
This is how I've been doing my annotation as I thought each tag was a
the root element for that class....
@RootElement
kml
@RootElement
document
@RootElement
Placemark
Will change it so hopefully all my other niggles will go away!
On Jan 12, 3:30 pm, Peter Knego <pe...@knego.net> wrote:
> Class Strong is not mapped to root element. It should be:
>
> class Strong{
> @Text
> public String text;
>
> }
>
> @RootElement("div")
> public class Root {
>
> @Element(name = "strong", targetType = Strong.class)
> @Text
> public List elements; // it'll contain both String and Strong
> classes
>
> }
>
> Peter
>
rootelement
element
subelement
childelement1
childelement2
childelement3
subelement
For some reason, the subelement seems to be returning null.
I'd mapped the subelements to a collection within element (there can
be multiple occurrences) and I'd mapped the childelements as follows:
@Element(name="name", targetType=childelement1.class),
@Element(name="description", targetType=childelement2.class),
@Element(name="Point", targetType=childelement3.class)
private List contents;
in other words
$RootElement
rootelement
{
$Element
Element element
}
Element
{
$Element("subelement", targetType=subelement.class)
List subelements
}
Subelement
{
@Element(name="name", targetType=childelement1.class),
@Element(name="description", targetType=childelement2.class),
@Element(name="Point", targetType=childelement3.class)
private List contents;
}
etc .....
Thanks in advance.
On Jan 12, 3:53 pm, Peter Knego <pe...@knego.net> wrote:
> You can mix-and-match named mappings with wildcard mappings:
>
> @RootElement("div")
> public static class Root {
>
> @Element("*")
> @Element(name = "strong", targetType = Strong.class)
> @Text
> public List elements;
>
> }
>
> This will map all XML text to Strings, all <strong> to Strong and all other
> XML subelements to DomElement.
>
> This approach is described inhttp://code.google.com/p/xmappr/wiki/PreservingUnmappedelements#Gradu...
>
> On Tue, Jan 12, 2010 at 4:47 PM, Peter Knego <pe...@knego.net> wrote:
> > Yes, @RootElement maps root XML element to a class, so it's only needed on
> > one class.
>
> > This test does something similar to what you are trying to do:
>
> >http://code.google.com/p/xmappr/source/browse/trunk/src/test/java/org...
>
> > The collection now has getters/setters.
>
> > Peter
>
@Element(name="description", targetType=childelement2.class),
@Element(name="Point", targetType=childelement3.class)
private List contents;
Cheers
On Jan 13, 9:59 am, Peter Knego <pe...@knego.net> wrote:
> Yes that's possible. Are you getting any Exceptions?
>
> I see now that I made a mistake with the example - multiple @Element
> annotations must be wrapped in @Elements() annotation:
>
> @Elements({
> @Element(name="name", targetType=childelement1.class),
>
> > @Element(name="description", targetType=childelement2.class),
> > @Element(name="Point", targetType=childelement3.class)
>
> })
>
> > private List contents;
>
> Also contents field needs getter/setter.
>
> If it still gives you trouble you can send me example code and XML in a zip
> file and I'll make it work.
>
Yes please, so send it.
On 13 Jan 2010 12:55, "Richard Hague" <rich...@googlemail.com> wrote:
Hi Peter,
I've written a unit test that mimics my xml mapping
structure. Can I send it to you please?
Cheers
On Jan 13, 9:59 am, Peter Knego <pe...@knego.net> wrote: > Yes that's possible. Are you getting any...
> On Wed, Jan 13, 2010 at 10:44 AM, Richard Hague <richha...@googlemail.com>wrote:
> > > Hi Peter, > > Should I be able to map: > > > rootelement > > e...
On Jan 13, 12:28 pm, Peter Knego <pe...@knego.net> wrote:
> Yes please, so send it.
>
Many thanks,
Richard
> >http://code.google.com/p/xmappr/wiki/NameSpaces#Namespaces_defined_in...
>
> > With this line added your example works.
>
> > Peter
>
Thanks for your help,
Richard
On Jan 13, 4:02 pm, Peter Knego <pe...@knego.net> wrote:
> here is the code..
>
> testxmappr 2.zip
> 25KViewDownload