Java XStream在普通xml上失败
我有一个非常简单的xml文件: testSimple2.txtJava XStream在普通xml上失败,java,xml,xstream,Java,Xml,Xstream,我有一个非常简单的xml文件: testSimple2.txt <root> <document> </document> </root> Main.java @XStreamAlias("root") public class Root { @XStreamAlias("document") static public class Document { } @XStreamAlias("document") Document
<root>
<document>
</document>
</root>
Main.java
@XStreamAlias("root")
public class Root {
@XStreamAlias("document")
static public class Document {
}
@XStreamAlias("document")
Document document;
}
主要代码:
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)
我知道肯定有一些简单的错误,但我看不出来。请提供帮助。可能是您缺少xml标题行:
<?xml version="1.0" encoding="utf-8"?>
<root>
<document>
</document>
</root>
但我知道XStream不应该需要它。我不敢相信我犯了这个错误。在第四行:
Root newJoe = (Root) xstream.fromXML(new File("testSimple2.txt"));
我所需要的只是删除“Root.class”,或者用实例替换它。在谷歌搜索和发布此类问题之前检查方法签名和文档还有一个原因…通常有两个
@XStreamAlias(“文档”)
注释吗?