Hi Petr,

Petr Adamec wrote:

> 
> Good morning,
> I have a problem with convert from xml to data (unmarshall) when I use
> xml which has unknown tags. Xstream write me Exception in thread "main"
> 
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException:
> No such field.

This is the expected behavior.

> I was looking for solution for my problem on internet. I find some it
> but all of them do not working on my case.
> 
> I override method wrapMapper but it still no working.
> 
> XStream xstream = new XStream() {
> 
>              // to enable ignoring of unknown elements
> 
>              @Override
> 
>              protected MapperWrapper wrapMapper(MapperWrapper next) {
> 
>                  return new MapperWrapper(next) {
> 
>                      @Override
> 
>                      public boolean shouldSerializeMember(Class definedIn,
>                      String fieldName) {
> 
>                          if (definedIn == Object.class) {
> 
>                              try {
> 
>                                  return this.realClass(fieldName) != null;
> 
>                              } catch (Exception e) {
> 
>                                  return false;
> 
>                              }
> 
>                          }
> 
>                          return super.shouldSerializeMember(definedIn,
>                          fieldName);
> 
>                      }
> 
>                  };
> 
>              }
> 
>          };
> 

Which version of XStream are you using? 
 
> Is it maybe because I have a collection at class. But every options
> which I try not works.
> 
> My class which I need convert to/from  is
> 
> public class Configurations implements Serializable{
> 
>      private static final long serialVersionUID = 4392719277296957995L;
> 
>      private List<Class<? extends Updatable>> tableNames;
> 
>      private Map<Class<? extends Updatable>, List<? extends Updatable>>
>      tables;
> 
>      ...
> 
>      }
> 
> 
> Updatable is only interface. I need  convert to/from xml every class
> which implements this interface.
> 
> In normal case, when I has right xml (get from marshell data) it is ok.
> I can convert data to this xml and xml back to data.
> 
> <mypackage.Configurations>
> 
>    <tableNames>
> 
>      <java-class>mypackage.configuration.MyClass</java-class>
> 
>    </tableNames>
> 
>    <tables>
> 
>      <entry>
> 
>        <java-class>mypackage.configuration.MyClass</java-class>
> 
>        <list>
> 
>          <mypackage.configuration.MyClass>
> 
>            <id>0</id>
> 
>            <created>2014-09-19 08:49:34.649 UTC</created>
> 
>            <active>true</active>
> 
>          </mypackage.configuration.MyClass>
> 
>        </list>
> 
>      </entry>
> 
>    </tables>
> 
> </mypackage.Configurations>
> 
> 
> But when I change xml (forr example add new tag newTag)
> 
> <mypackage.Configurations>
> 
>    <tableNames>
> 
>      <java-class>mypackage.configuration.MyClass</java-class>
> 
>    </tableNames>
> 
>    <tables>
> 
>      <entry>
> 
>        <java-class>mypackage.configuration.MyClass</java-class>
> 
>        <list>
> 
>          <mypackage.configuration.MyClass>
> 
>            <id>0</id>
> 
>            <created>2014-09-19 08:49:34.649 UTC</created>
> 
>            <active>true</active>
> 
>            <newTag>exam</newTag>
> 
>          </mypackage.configuration.MyClass>
> 
>        </list>
> 
>      </entry>
> 
>    </tables>
> 
> </mypackage.Configurations>
> 
> 
> Xstream ends with error
> AbstractReflectionConverter$UnknownFieldException: No such field. Is
> exists solution how can I solve this problem? I need convert to data
> (class) and if xml has unknown tags which does not exist at class, logg
> some message and skip tag.

See http://xstream.codehaus.org/faq.html#Serialization_newer_class_versions

The topics also direct you to the proper location of the custom mapper from 
above.

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to