java-尝试使用xstream将xml响应转换为pojo时出错

java-尝试使用xstream将xml响应转换为pojo时出错,java,google-app-engine,Java,Google App Engine,我在java web应用程序中收到以下代码的错误-- 该错误显示在该行正上方的代码行-- error=“类型不匹配-无法从对象转换为apiresponse” 下面是我必须解析的XML--- http://ahrefs.com/robot/ http://blog.ahrefs.com/ 50.22.24.236 Ahrefs–反向链接研究工具 2011-08-31T07:56:53Z 博客 257.674000 文本 假的 http://apps.vc/ http://ahrefs.com/r

我在java web应用程序中收到以下代码的错误--

该错误显示在该行正上方的代码行-- error=“类型不匹配-无法从对象转换为apiresponse”

下面是我必须解析的XML---


http://ahrefs.com/robot/
http://blog.ahrefs.com/
50.22.24.236
Ahrefs–反向链接研究工具
2011-08-31T07:56:53Z
博客
257.674000
文本
假的
http://apps.vc/
http://ahrefs.com/robot/
64.20.55.86
设备信息
2011-08-27T18:59:31Z
http://ahrefs.com/robot/
209.787100
文本
假的
我创建了以下java类来从上述xml获取数据---

package com.arvindikchari.linkdatasmith.domain;
最终公共类apiresponse{
受保护的结果集链接和链接;
公众回应({
}
公共结果集链接getRlinks()
{
返回rlinks;
}
公共设置链接(结果集链接)
{
this.rlinks=rlinks;
}
}  
最终公共类结果集链接{
受保护列表indiv_result=new ArrayList();
公共结果集_链接(){
}
公共列表getIndiv_结果()
{
退货清单;
}
公共无效设置独立结果(列出独立结果)
{
this.indiv_result=indiv_result;
}
}  
公开课最终成绩{
受保护的字符串源url;
受保护的字符串目的地地址;
受保护的字符串源ip;
受保护的字符串源标题;
访问受保护字符串;
保护绳锚;
保护串额定值;
受保护的字符串链接类型;
公开结果(){
}
公共字符串getSource_url()
{
返回源url;
}
公共无效设置源url(字符串源url)
{
this.source\u url=source\u url;
}
公共字符串getDestination_url()
{
返回目的地地址;
}
public void setDestination\u url(字符串destination\u url)
{
this.destination\u url=destination\u url;
}
公共字符串getSource_ip()
{
返回源ip;
}
公共无效设置源ip(字符串源ip)
{
this.source\u ip=source\u ip;
}
公共字符串getSource_title()
{
返回源标题;
}
公共无效设置源标题(字符串源标题)
{
this.source\u title=source\u title;
}
公共字符串getVisited()
{
回访;
}
已访问公共void集合(已访问字符串)
{
这个.访问过的=访问过的;
}
公共字符串getAnchor()
{
回锚;
}
公共无效设置锚(字符串锚)
{ 
这个。锚=锚;
}
公共字符串getRating()
{
回报率;
}
公共无效设置等级(字符串等级)
{ 
这个。评级=评级;
}
公共字符串getLink_type()
{
返回链接类型;
}
公共无效设置链接类型(字符串链接类型)
{
this.link\u type=link\u type;
}
}

我做错了什么?

您有很多错误,但与您的消息相对应的错误是您必须将xstream.fromXML的结果强制转换为apiresponse的对象:

apiresponse result = (apiresponse)xstream.fromXML(resp);
此外,您提供的代码(Java类)没有编译,存在许多错误

以下是一些改进:

Result.java

@XStreamAlias("result")
public class Result {

   protected String source_url;
   protected String destination_url;
   protected String source_ip;
   protected String source_title;
   protected String visited;
   protected String anchor;
   protected String rating;
   protected String link_type;
   protected Boolean is_nofollow;

public Result() {

}

public String getSource_url()
{

return source_url;
}

public void setSource_url(String source_url)
{

this.source_url=source_url;
}

public String getDestination_url()
{

return destination_url;
}

public void setDestination_url(String destination_url)
{

this.destination_url=destination_url;
}

public String getSource_ip()
{

return source_ip;
}

public void setSource_ip(String source_ip)
{

    this.source_ip=source_ip;
}


public String getSource_title()
{

return source_title;
}

public void setSource_title(String source_title)
{

this.source_title=source_title;
}


public String getVisited()
{

return visited;
}

public void setVisited(String visited)
{

this.visited=visited;
}


public String getAnchor()
{

return anchor;
}

public void setAnchor(String anchor)
{

this.anchor=anchor;
}


public String getRating()
 {

return rating;
 }

public void setRating(String rating)
{

this.rating=rating;
}


public String getLink_type()
{

return link_type;
}

public void setLink_type(String link_type)
{

    this.link_type=link_type;
}

public Boolean getIs_nofollow() {
    return is_nofollow;
}

public void setIs_nofollow(Boolean is_nofollow) {
    this.is_nofollow = is_nofollow;
}
@XStreamAlias("resultset_links")
public class ResultsetLinks {

@XStreamImplicit(itemFieldName="result")
protected List<Result> indivResult = new ArrayList<Result>();

  public ResultsetLinks() {

  }

  public List<Result> getResult()
  {

    return indivResult;
  }


  public  void setResult(List<Result> indiv_result)
    {

        this.indivResult =indiv_result;
    }

}
@XStreamAlias("apiresponse")
public class ApiResponse {

@XStreamAlias("resultset_links")
protected ResultsetLinks rlinks;


public ApiResponse() {

}

public ResultsetLinks getRlinks()
{
    return rlinks;
}

public void setRlinks(ResultsetLinks rlinks)
  {
    this.rlinks=rlinks;
  }
ResultsetLinks.java

@XStreamAlias("result")
public class Result {

   protected String source_url;
   protected String destination_url;
   protected String source_ip;
   protected String source_title;
   protected String visited;
   protected String anchor;
   protected String rating;
   protected String link_type;
   protected Boolean is_nofollow;

public Result() {

}

public String getSource_url()
{

return source_url;
}

public void setSource_url(String source_url)
{

this.source_url=source_url;
}

public String getDestination_url()
{

return destination_url;
}

public void setDestination_url(String destination_url)
{

this.destination_url=destination_url;
}

public String getSource_ip()
{

return source_ip;
}

public void setSource_ip(String source_ip)
{

    this.source_ip=source_ip;
}


public String getSource_title()
{

return source_title;
}

public void setSource_title(String source_title)
{

this.source_title=source_title;
}


public String getVisited()
{

return visited;
}

public void setVisited(String visited)
{

this.visited=visited;
}


public String getAnchor()
{

return anchor;
}

public void setAnchor(String anchor)
{

this.anchor=anchor;
}


public String getRating()
 {

return rating;
 }

public void setRating(String rating)
{

this.rating=rating;
}


public String getLink_type()
{

return link_type;
}

public void setLink_type(String link_type)
{

    this.link_type=link_type;
}

public Boolean getIs_nofollow() {
    return is_nofollow;
}

public void setIs_nofollow(Boolean is_nofollow) {
    this.is_nofollow = is_nofollow;
}
@XStreamAlias("resultset_links")
public class ResultsetLinks {

@XStreamImplicit(itemFieldName="result")
protected List<Result> indivResult = new ArrayList<Result>();

  public ResultsetLinks() {

  }

  public List<Result> getResult()
  {

    return indivResult;
  }


  public  void setResult(List<Result> indiv_result)
    {

        this.indivResult =indiv_result;
    }

}
@XStreamAlias("apiresponse")
public class ApiResponse {

@XStreamAlias("resultset_links")
protected ResultsetLinks rlinks;


public ApiResponse() {

}

public ResultsetLinks getRlinks()
{
    return rlinks;
}

public void setRlinks(ResultsetLinks rlinks)
  {
    this.rlinks=rlinks;
  }
}

最后,您的代码将对XML进行解组:

    XStream xstream = new XStream();
    xstream.processAnnotations(ApiResponse.class);
    xstream.processAnnotations(ResultsetLinks.class);
    xstream.processAnnotations(Result.class);

    ApiResponse result = (ApiResponse)xstream.fromXML(resp);
所有这些代码在Xstream 1.4.2中运行良好

尝试按照Sun的编码约定来编写类名、属性名等。。。
使用XStreamAlias将Java类名调整为XML名称。

谢谢-现在我清楚地了解了如何使用xstream。。。不幸的是,xstream中存在一些与google app engine for java不兼容的受限类……因此,现在我必须寻找另一个解析器:(