gpt4 book ai didi

xstream - Xstream 中的继承问题

转载 作者:行者123 更新时间:2023-12-02 02:16:10 25 4
gpt4 key购买 nike

我有一个输入 xml 和相应的 java 类,如下所示。

<Address>
<name>name</name>
<streetName>street</streetName>
<Address>

public class Address {
private String name;
//getter and setter here
}

public class Home extends Address {
private String streetName;
// getter and setter follows
}

然后我尝试像下面这样解码 xml

import com.thoughtworks.xstream.XStream;

public class Main {
public static void main(String as[]) {
XStream xstream = new XStream();
String str = "<com.xstream.inheritance.Address> <name>name</name> <streetName>street</streetName>"
+ "</com.xstream.inheritance.Address>";
Address address1 = (Address) xstream.fromXML(str);
System.out.println(address1.getName());
}
}

我有一个异常(exception)说线程“main”中的异常

com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.xstream.inheritance.Address.streetName
---- Debugging information ----
field : streetName
class : com.xstream.inheritance.Address
required-type : com.xstream.inheritance.Address
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /com.xstream.inheritance.Address/streetName
version : null

为什么我得到这个异常?

最佳答案

XML 缺少指向扩展类的“​​类”属性。所以输出将是这样的:

<com.xstream.inheritance.Address class="com.xstream.inheritance.Home">
<name>name</name>
<streetName>street</streetName>
</com.xstream.inheritance.Address>

这是由 XStream 自动完成的。

关于xstream - Xstream 中的继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10498546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com