【问题标题】:Deserializing a XML to Object with xstream when an attribute is generic当属性是通用的时,使用 xstream 将 XML 反序列化为对象
【发布时间】:2012-08-09 14:30:32
【问题描述】:

我从客户端收到以下 XML:

<data>
    <action>someAction</action>  // actionX, actionY, and so forth...
    <params>
        <name>Some name</name>
        <tel>1234567890</tel>
        .
        .
        .
        etc...
    </params>
</data>

我创建了以下类:

class Data<T> {
    String action;
    T params;
}

class ContentX {
    String name;
    String tel;
}

class ContentY {
    String id;
    String productDesc;
}

我需要使用 Data 类将 XML 转换为对象。 根据操作,我需要将 T 属性映射到特定的类,例如 ContentX 或 ContentY。

作为示例,我已经尝试过了,但它不起作用:

Data<ContentX> data = (Data<ContentX>) xstream.fromXML(XML);

我收到以下异常:

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field java.lang.Object.name
---- Debugging information ----
field               : name
class               : java.lang.Object
required-type       : java.lang.Object
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /data/params/name
line number         : 1
class[1]            : com.xstream.xml.Data
version             : null
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1058)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:913)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:904)

【问题讨论】:

    标签: java xml generics xstream


    【解决方案1】:
        XStream xstream = new XStream();
        Data<ContentX> data = new Data<ContentX>();
        data.action="push";
        data.params=new ContentX();
        data.params.name="where";
        data.params.tel="1712";
    
        xstream.alias("data", Data.class);
        xstream.alias("params", ContentX.class);
        String xml = xstream.toXML(data);
        System.out.println(xml);
    
        Data<ContentX> result = (Data<ContentX>)xstream.fromXML(xml);
        System.out.println(result.action);
        System.out.println(result.params.name);
        System.out.println(result.params.tel);
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2015-04-13
      • 2011-03-09
      • 1970-01-01
      • 2019-03-29
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多