Java 中的 XSTREAM API 在 XML 解析中抛出错误

xstream api in java throwing error in xml parsing

提问人:shwetabh 提问时间:9/25/2023 最后编辑:shwetabh 更新时间:9/25/2023 访问量:21

问:

我已使用xstream api来解绑xml消息,并且代码总是失败

代码块:

        XStream xStream=new XStream();
        xStream.processAnnotations(ProjectDescription.class);
        xStream.processAnnotations(Projects.class);
        xStream.ignoreUnknownElements();
        System.out.println("Unmarshalling begins...");
        ProjectDescription appDesp = (ProjectDescription)         xStream.fromXML("checkxml.xml");

实体类

** 项目描述.class:**


@XStreamAlias("projectDescription")
public class ProjectDescription {
    @XStreamAlias("name")
    private String name;

    @XStreamAlias("comment")
    private String comment;

    @XStreamImplicit
    private List<Projects> projects = new ArrayList<Projects> ();




}

项目 .class:

@XStreamAlias("projects")
public class Projects {
     @XStreamImplicit
     private List<String> project= new ArrayList<String>();


}

示例 XML:

<projectDescription>
    <name>SimpleApp</name>
    <comment></comment>
    <projects>
        <project>Sampleshare</project>
        <project>Sampleshare2</project>
        <project>Samplestatic</project>
    </projects>
</projectDescription>

它出错并显示消息:

引起:org.xmlpull.v1.XmlPullParserException:在开始标记之前只允许空格内容,不允许 C(位置:START_DOCUMENT看到 C... @1:2)

有人可以指导我哪里出了问题吗?

我已经尝试了 DOM 解析器和 XStream api,但上面所述的相同示例消息适用于 DOM 而不是 XStream。

Java XML DOM XTrome

评论


答: 暂无答案