Sorry if this is an answer that show how little I know of XStream; I cant
find the ListConverter in the XStream API...?
Jörg Schaible-2 wrote:
>
> Hi,
>
> Villemos wrote:
>
>>
>> I'm new to XStream and XML to POJO conversion, so this problem is
>> properly
>> quite simple and stupid.
>>
>> I have the following XML string;
>>
>> <response>
>> <status_code>0</status_code>
>> <result>
>> <item>
>> <fieldset>General information</fieldset>
>> <description>General document information</description>
>> <fields>
>> <item>
>> <name>Document Author</name>
>> <value>John Doe</value>
>> </item>
>> <item>
>> <name>Reference ID</name>
>> <value>OSMV-OPMT-LOGI-RP-11-1402</value>
>> </item>
>> </fields>
>> </item>
>> </result>
>> </response>
>>
>>
>> And have created the following classes;
>>
>> @XStreamAlias("response")
>> public class Metadata {
>>
>> public String status_code = "";
>>
>> @XStreamAlias("result")
>> public List<MetadataItem> result = new ArrayList<MetadataItem>();
>
> This alias is superfluous, the field name is already "result".
>
>> }
>>
>>
>> @XStreamAlias("item")
>> public class MetadataItem {
>>
>> public String fieldset = "";
>> public String description = "";
>>
>> @XStreamAlias("fields")
>> public List<MetadataField> fields = new ArrayList<MetadataField>();
>> }
>>
>>
>> @XStreamAlias("item")
>> public class MetadataField {
>
> As you found out, this does not work. You cannot assign the same alias to
> different types. This has to be a 1:1 relation. With this definition, your
> program behaves non-deterministic. Depending on the sequence of
> registration, all "item" elements will be deserialized to Metadata or
> MetadataField types.
>
>> public String name = "";
>> public String value = "";
>> }
>>
>>
>>
>>
>> Running the following junit tests fails misserably;
>>
>> public class MetadataTest extends TestCase {
>>
>> public void testConvert() {
>> String testString = ...
>>
>> Metadata metadata = new Metadata();
>> XStream xstream = new XStream();
>> xstream.processAnnotations(MetadataField.class);
>> xstream.processAnnotations(MetadataItem.class);
>> xstream.processAnnotations(Metadata.class);
>> xstream.fromXML(testString, metadata);
>> }
>> }
>>
>> The trace is shown below. I dont understand the error message; It looks
>> like its in the MetadataField:name conversion that something goes wrong,
>> but thats a String. How can a CannotResolveClassException be thrown?
>>
>>
>>
>> com.thoughtworks.xstream.converters.ConversionException: name : name :
>> name
>> : name
>> ---- Debugging information ----
>> message : name : name
>> cause-exception :
>> com.thoughtworks.xstream.mapper.CannotResolveClassException
>> cause-message : name : name
>> class : com.logica.oam.ktree.converters.Metadata
>> required-type : com.logica.oam.ktree.converters.MetadataItem
>> path : /response/result/item/fields/item/name
>> line number : 1
>> -------------------------------
>
> It tries to create a Metadata instance, but that one has no field "name".
>
> You should remove both item aliases and use local ListConverters where you
> define that all elements are of the required type and should use the alias
> 'item' in the list. See the ListConverter constructor and
> XStream.registerLocalConverter.
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
>
--
View this message in context:
http://old.nabble.com/Getting-CannotResolveClassException-in-simple-XML2POJO-example-tp33308266p33317856.html
Sent from the xstream - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email