Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当属性为泛型时,使用xstream将XML反序列化为对象_Java_Xml_Generics_Xstream - Fatal编程技术网

Java 当属性为泛型时,使用xstream将XML反序列化为对象

Java 当属性为泛型时,使用xstream将XML反序列化为对象,java,xml,generics,xstream,Java,Xml,Generics,Xstream,我正在从客户端接收以下XML: <data> <action>someAction</action> // actionX, actionY, and so forth... <params> <name>Some name</name> <tel>1234567890</tel> . . .

我正在从客户端接收以下XML:

<data>
    <action>someAction</action>  // actionX, actionY, and so forth...
    <params>
        <name>Some name</name>
        <tel>1234567890</tel>
        .
        .
        .
        etc...
    </params>
</data>
XStream XStream=newxstream();
数据=新数据();
data.action=“推送”;
data.params=new ContentX();
data.params.name=“where”;
data.params.tel=“1712”;
别名(“数据”,data.class);
别名(“params”,ContentX.class);
字符串xml=xstream.toXML(数据);
System.out.println(xml);
数据结果=(数据)xstream.fromXML(xml);
System.out.println(结果.动作);
System.out.println(result.params.name);
System.out.println(result.params.tel);
class Data<T> {
    String action;
    T params;
}

class ContentX {
    String name;
    String tel;
}

class ContentY {
    String id;
    String productDesc;
}
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)
    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);