所以我正在尝试使用xStream从XML创建一个类(XMLPanelList),但是当我运行程序时出现这个错误。
<panels> <panel> <name>Test 1</name> <component> <name>TestComponant</name> <type>0</type> </component> </panel> <panel> <name>Test 2</name> <component> <name>TestComponant2</name> <type>1</type> </component> </panel></panels>
com.thoughtworks.xstream.converters.ConversionException: Cannot deserialize object with new readObject()/writeObject() methods
---- Debugging information ----
class : XMLPanel
required-type : XMLPanel
converter-type : com.thoughtworks.xstream.converters.reflection.SerializableConverter
path : /panels/panel
class[1] : XMLPanelList
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version : 1.4.7
-------------------------------
at com.thoughtworks.xstream.converters.reflection.SerializableConverter.doUnmarshal(SerializableConverter.java:333)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:257)
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.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:377)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:257)
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:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1031)
at XStreamTest.main(XStreamTest.java:64)
我不知道如何修复它,谷歌什么也没告诉我。这是XML文件“test.xml”
<panels>
<panel>
<name>Test 1</name>
<component>
<name>TestComponant</name>
<type>0</type>
</component>
</panel>
<panel>
<name>Test 2</name>
<component>
<name>TestComponant2</name>
<type>1</type>
</component>
</panel>
</panels>
以下是代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;
import javax.swing.JFrame;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
class XMLPanelList {
Vector<XMLPanel> xmlPanels;
public XMLPanelList(){ };
}
class XMLPanel extends JFrame{
String name;
Vector<XMLComponent> xmlComps;
public XMLPanel(){
super("XMLPanel");
}
}
class XMLComponent {
String name;
int type;
public XMLComponent(){}
public XMLComponent(String name, int type){
this.name = name;
this.type = type;
}
}
public class XStreamTest {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new FileReader(new File("test.xml")));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
System.out.println(everything);
br.close();
XStream xstream = new XStream(new DomDriver());
xstream.alias("panels", XMLPanelList.class);
xstream.alias("panel", XMLPanel.class);
xstream.alias("component", XMLComponent.class);
xstream.addImplicitArray(XMLPanelList.class, "xmlPanels", "panel");
xstream.addImplicitArray(XMLPanel.class, "xmlComps", "component");
XMLPanelList xmlTrans = (XMLPanelList) xstream.fromXML(everything);
System.out.println(xstream.toXML(xmlTrans));
}
catch(Exception e){
e.printStackTrace();
}
}
}
如何修复给出的错误?
答案 0 :(得分:1)
从标准java.util.List切换到专用列表时,我遇到了类似的问题。在将XML文件的根元素更改为:
时,请尝试查看对象是否加载<panels serialization="custom">
<panels>
<default/>
<int>[TOTAL-NUMBER-OF-PANELS]</int>
<panel>
<name>Test 1</name>
<component>
<name>TestComponant</name>
<type>0</type>
</component>
</panel>
<panel>
<name>Test 2</name>
<component>
<name>TestComponant2</name>
<type>1</type>
</component>
</panel>
</panels>
</panels>
祝你好运。
答案 1 :(得分:-4)
不熟悉库,但从错误文本判断,实现XML组件的可序列化接口。