当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Cannot deserialize object with new readObject()/writeObject() methods

问题描述:

So I'm trying to create a class (XMLPanelList) from XML using xStream, but I'm getting this error when I run my program.

<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)

I have no idea how to fix it, and Google tells me nothing. Here is the XML file "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>

And here is the code:

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();

}

}

}

How do I fix the error given?

网友答案:

I've had a similar problem when switching from standard java.util.List to a specialized list. Try and see if the object loads when you change the root element of your XML file to:

<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>

Good luck.

网友答案:

Not famaliar with library but judging from error text, Implement the serializable interface for your XML components.

分享给朋友:
您可能感兴趣的文章:
随机阅读: