ntlcn / xmappr

Automatically exported from code.google.com/p/xmappr
0 stars 0 forks source link

SubElement name="*" does not work #28

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What is the expected output? What do you see instead?
class SubElement{
@Element(name="*", targetType=String.class) 
List numbers;
}
does not work, it regards numbers the type DomElement any way, while

class SubElement{
@Element(name="Date", targetType=String.class) 
List numbers;
} 
works.

What version of the product are you using? On what operating system?
0.93

Original issue reported on code.google.com by elgs1...@gmail.com on 15 Feb 2010 at 7:31

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Actually the documentation is wrong and the code is correct.

'targetType' is currently ignored when wildcard mapping ("*") is used. The 
correct
behaviour would be that it should throw an exception if 'targetType' is used.

Wildcard mappings were designed so that different XML elements could be mapped 
to the
same field (usually collection). To make this work there must be a way for 
Xmappr to
know which XML elements to produce when serializing (producing XML). For 
instance if
different XML elements are all mapped to Integers (and added to Collection), 
then
there is no way to know which XML elements to produce when serializing. 

That's why DomElement was introduced. It contains the XML element name and
value, so it can be correctly serialized. 

The other way is to define the custom converter via 'converter' attribute. In 
this
case custom converter must have it's own way to create XML elements based on 
object
values. NumbersConverter in unit test WildcardMappingTest.java is one
such example.

I will correct code to throw an exception of 'targetType' is used on wildcard 
mapping. 

If all your subelements are <date> than it's correct to use: 

class SubElement{
@Element(name="Date", targetType=String.class) 
List numbers;
} 

or even more simple (type inferred via collection's generic parameter type):

class SubElement{
@Element("Date") 
List<String> numbers;
} 

Original comment by peter.kn...@gmail.com on 15 Feb 2010 at 11:06

GoogleCodeExporter commented 8 years ago
Thank you for a quick response. I actually want to put all elements under the 
subelement to a list of Strings. I want all them converted to String. But it 
seems 
they can just be converted to DomElement. When I use the code:
{{{
    @Element(name = "*")
    public List<String> fields;
}}} 
I got the exception:
{{{
Exception in thread "main" java.lang.ClassCastException: org.xmappr.DomElement 
cannot 
be cast to java.lang.String
}}}

But anyway, I can do it like this:
{{{
    @Element(name = "*")
    public List<DomElement> fields;
}}}
And,
{{{
DomElement.getElements().get(0)
}}}

Thank you very much,
Elgs

Original comment by elgs1...@gmail.com on 16 Feb 2010 at 7:49

GoogleCodeExporter commented 8 years ago

Original comment by peter.kn...@gmail.com on 7 Mar 2010 at 2:16