使用xStream库将XML解析为POJO

时间:2015-05-07 03:05:11

标签: xml xstream

我使用Xstream libray来解析xml,但是有一个例外:com.thoughtworks.xstream.converters.ConversionException:类型为com.mycontacts.model.explain32.Title32的元素标题未定义为java.lang类型中的字段。对象

explain32.xml

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

<data>
<title name="123"/>
</data>

<data>
<title name="234"/>
</data>

</explain>

java bean:

public class ExplainXmlBody32 {
    private List<ExplainXmlData32> data = new ArrayList<ExplainXmlData32>();
}

public class ExplainXmlData32 {
    private Title32 title;
}
public class Title32 {
     String name;
}

我解析xml如:

    private void parseXML_explain32(){
        try{
            String filePath = Environment.getExternalStorageDirectory() + "/myxml/explain32.xml";
            String strXML = IOUtils.toString(new FileInputStream(new File(filePath)),  "utf-8");
            tvShow.setText(strXML);

            XStream xStream = new XStream();

            xStream.alias("explain", ExplainXmlBody32.class);
            xStream.alias("data", ExplainXmlData32.class);          
            xStream.alias("title", Title32.class);

//          xStream.aliasField("title", ExplainXmlData32.class, "title");
            xStream.aliasAttribute(Title32.class, "name", "name");

            xStream.addImplicitCollection(ExplainXmlBody32.class, "data");
            //          xStream.addImplicitCollection(ExplainXmlData32.class, "titles");

            //           XOAlias.configExplainXml(xStream);

            ExplainXmlBody32 explain32 = (ExplainXmlBody32) xStream.fromXML(strXML);

            System.out.println("explain32, size:" + explain32.getData().size());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

异常信息:

com.thoughtworks.xstream.converters.ConversionException: Element title of type com.mycontacts.model.explain32.Title32 is not defined as field in type java.lang.Object
---- Debugging information ----
class               : com.mycontacts.model.explain32.ExplainXmlBody32
required-type       : java.lang.Object
path                : /explain/data/title
line number         : 5

如何改进我的代码并解决这个问题?

2 个答案:

答案 0 :(得分:0)

试试这个。将ArrayList更改为List。还为XML类型添加了定义。需要数据和说明,因为它没有创建类名并使其成为元素,因此代码不会创建其他标记。

&#13;
&#13;
   [XmlRoot("explain")]
public class ExplainXmlBody32
{
    [XmlElement("data")]
    private List<ExplainXmlData32> data = new List<ExplainXmlData32>();
}

public class ExplainXmlData32
{
    [XmlElement("title")]
    private Title32 title;
}
public class Title32
{
    [XmlAttribute("name")]
    String name;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我找到了解决这个问题的方法。

更改java bean:

public class ExplainXmlBody32 {
    private List<ExplainXmlData32> data = new ArrayList<ExplainXmlData32>();
}

public class ExplainXmlBody32 {
    private List<ExplainXmlData32> datas = new ArrayList<ExplainXmlData32>();
}

然后

方法parseXML_explain32()中的

: 变化

xStream.addImplicitCollection(ExplainXmlBody32.class, "data");

xStream.addImplicitCollection(ExplainXmlBody32.class, "datas");

就是这样。