簡體   English   中英

XStream在瑣碎的xml上失敗

[英]XStream fail on trivial xml

我有一個非常簡單的xml文件:

testSimple2.txt

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

但是由於某種原因,我無法使用XStream反序列化它。

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

我知道肯定有一些簡單的錯誤,但我看不到。 請幫忙。

我不敢相信我犯了這個錯誤。 在第四行:

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

我所需要的只是刪除“ Root.class”,或將其替換為實例。 在谷歌搜索和發布此類問題之前,還要檢查一下方法簽名和文檔的另一個原因...

可能是您缺少xml標頭行:

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

但是我知道XStream不需要這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM