0

私は非常に単純なxmlファイルを持っています:

testSimple2.txt

<root>
  <document>
  </document>
</root>

しかし、何らかの理由で、XStreamで逆シリアル化できません。

Root.java

@XStreamAlias("root")
public class Root {

 @XStreamAlias("document")
 static public class Document {
 }
 @XStreamAlias("document")
 Document document;

}

Main.java

メインコード:

XStream xstream = new XStream();
xstream.autodetectAnnotations(true);
xstream.processAnnotations(Root.class);
Root newJoe = (Root) xstream.fromXML(new File("testSimple2.txt"), Root.class); //Exception here

次の例外がスローされます。

com.thoughtworks.xstream.converters.ConversionException: Element document of type verySimple.Root$Document is not defined as field in type java.lang.Class
---- Debugging information ----
class               : verySimple.Root
required-type       : verySimple.Root
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /root/document
line number         : 3
version             : null
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.writeValueToImplicitCollection(AbstractReflectionConverter.java:403)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:334)
    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:1006)
    at verySimple.RootProcess.main(RootProcess.java:26)

簡単な間違いがあるはずですが、見えません。助けてください。

4

2 に答える 2

1

私はこの間違いをしたとは信じられません。それは4行目にあります:

Root newJoe = (Root) xstream.fromXML(new File("testSimple2.txt"));

必要なのは、「Root.class」を削除するか、インスタンスに置き換えることだけでした。そのような質問をグーグルして投稿する前に、メソッドのシグネチャとドキュメントを確認するもう1つの理由...

于 2012-09-23T13:03:03.530 に答える
0

xmlヘッダー行が欠落している可能性があります。

<?xml version="1.0" encoding="utf-8"?>
<root>
  <document>
  </document>
</root>

しかし、私はXStreamがそれを必要としないことになっていることを知っています。

于 2012-09-22T23:52:11.367 に答える