繁体   English   中英

例外-使用XStream将复杂的XML转换为Object时抛出“序言中不允许内容”

[英]Exception - “Content is not allowed in prolog” throwing while convert the complex XML to Object using XStream

我有想要将其转换为对象的XML文件。 XML如下

<Response>
     <result xsi:type="TestResponse">
        <description xsi:type="xsd:string">Hello world</description>
        <name xsi:type="xsd:string">John</name>
        <success xsi:type="xsd:boolean">true</success>
     </result>
  </Response>

我尝试过这种方法

XStream stream = new XStream(new DomDriver());
stream.alias("result", Response.class); 
Response response = (Response) stream.fromXML(xmlStr);

It's throwing exception like :   `Content is not allowed in prolog`

Response.java就像

public class Response{
  private String description;
  private String name;
  private boolean success;

//setter and getter methods
}

您的问题很可能是由XML开头的字符引起的。 这些不一定是可见的,可能会卡住字节顺序标记(BOM)。 如果您使用不擅长编码的编辑器(Windows编辑器,记事本或类似工具)打开XML,则可能会发生这种情况。

尝试将其专门保存为UTF-8,然后添加

<?xml version="1.0" encoding="UTF-8"?>

最重要的是,这可能会为您解决。

如果那是完整的xml文件,则缺少标题:

例如:

<?xml version="1.0" encoding="UTF-8"?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM