gpt4 book ai didi

java - XStream - XML 到 arrayList

转载 作者:行者123 更新时间:2023-11-30 06:27:24 25 4
gpt4 key购买 nike

我对 Java 还很陌生。我有一个 XML 文件,我设法使用 xStream.toXML 从 ArrayList 创建该文件。

我的 Client 类具有以下对象:

  • 字符串:名称、ID

  • 整数:余额

  • ArrayList:电话

  • ArraList:bestDay

  • 项目(自定义类):数据

我的 Item 类具有以下对象:

  • 字符串:名称

  • 整数:价格

XML 文件如下所示:

<list>
<client>
<name>Client 1</name>
<id>054.658.547-74</id>
<phone>
<string>99564-5412</string>
<string>7854-5478</string>
</phone>
<bestDay>
<int>10</int>
</bestDay>
<balance>-20.0</balance>
<data>
<item>
<name>Item 1</name>
<price>5.0</price>
</item>
<item>
<name>Item 2</name>
<price>3.0</price>
</item>
<item reference="../item"/>
<item reference="../item[2]"/>
</data>
</client>
<client>
<name>Client 2</nome>
<id>047.654.987-78</id>
<phone>
<string>99564-1475</string>
<string>98544-5254</string>
</phone>
<bestDay>
<int>15</int>
<int>30</int>
</bestDay>
<balance>-15.0</balance>
<data>
<item>
<name>Item 3</name>
<price>4.0</price>
</item>
<item reference="../item"/>
<item reference="../item"/>
<item reference="../item"/>
<item reference="../item"/>
</data>
</client>
</list>

现在,当我尝试再次将此代码放入 ArrayList 时,使用以下代码:

XStream xStream = new XStream();
String rawXML = new String(Files.readAllBytes(Paths.get("output.xml")));
ArrayList<Client> = (ArrayList<Client>)xStream.fromXML(rawXML);

我在最后一行收到错误。

编辑:

好的,我只添加到构建路径:

  • xstream-1.4.10.jar

  • xpp3_min-1.1.4c.jar

现在我添加了 xmlpull-1.1.3.1.jar 并且出现另一个错误:

Security framework of XStream not initialized, XStream is probably vulnerable.
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : cliente
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /list/cliente
line number : 2
version : 1.4.10
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
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:1486)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1466)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1337)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1328)
at application.Cliente.load(Cliente.java:88)
at application.Main.main(Main.java:40)
... 11 more
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: cliente
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
at com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:71)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:125)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:72)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:98)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:91)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:85)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:80)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 22 more
Exception running application application.Main

最佳答案

检查你的类路径classnotfounderror 大多数时候是一个指示符,表明所需的类不在类路径中

关于java - XStream - XML 到 arrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46895817/

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