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

标签 java google-app-engine

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

         XStream xstream = new XStream();  
         apiresponse myClassObject;
         myClassObject= xstream.fromXML(resp);  

此行上方的代码行显示错误 - error="类型不匹配 - 无法从对象转换为 apiresponse"

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

<apiresponse version="1" xmlns="http://ahrefs.com/schemas/api/links/1">
 <resultset_links count="2">
  <result>
    <source_url>http://ahrefs.com/robot/</source_url>
    <destination_url>http://blog.ahrefs.com/</destination_url>
    <source_ip>50.22.24.236</source_ip>
    <source_title>Ahrefs – backlinks research tool</source_title>
    <visited>2011-08-31T07:56:53Z</visited>
    <anchor>Blog</anchor>
    <rating>257.674000</rating>
    <link_type>text</link_type>
    <is_nofollow>false</is_nofollow>
  </result>
  <result>
    <source_url>http://apps.vc/</source_url>
    <destination_url>http://ahrefs.com/robot/</destination_url>
    <source_ip>64.20.55.86</source_ip>
    <source_title>Device info</source_title>
    <visited>2011-08-27T18:59:31Z</visited>
    <anchor>http://ahrefs.com/robot/</anchor>
    <rating>209.787100</rating>
    <link_type>text</link_type>
    <is_nofollow>false</is_nofollow>
  </result>
 </resultset_links>
</apiresponse>

我创建了以下java类来从上面的xml获取数据---

package com.arvindikchari.linkdatasmith.domain;

final public class apiresponse {  

  protected resultset_links rlinks;  


  public apiresponse() {

  }

  public resultset_links getRlinks()
  {
    return rlinks;
  }

  public setRlinks(resultset_links rlinks)
    {
        this.rlinks=rlinks;
    }


}  



final public class resultset_links {  

    protected List<result> indiv_result = new ArrayList<result>();

  public resultset_links() {

  }


  public List<result> getIndiv_result()
  {

    return List;
  }


  public  void setIndiv_result(List<result> indiv_result)
    {

        this.indiv_result=indiv_result;
    }

}  

final 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;

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;
}


}

我在这里做错了什么?

最佳答案

您有很多错误,但与您的消息相对应的错误是您必须将 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;
}

ResultsetLinks.java:

@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;
    }

}

ApiResponse.java:

@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 的类名称、属性名称等编码约定... 使用 XstreamAliases 使 Java 类名适应 XML 名称。

关于java-当我尝试使用 xstream 将 xml 响应转换为 pojo 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518220/

相关文章:

java - 在 eclipse web 项目(小程序)中找不到类异常

java - 将图像插入 MySQL 数据库

java - Gradle munges 目录名称,同时创建包含所有依赖项的 jar

python - 将 Sqlite 数据导入 Google App Engine

google-app-engine - Google Drive API - 太慢了。

google-app-engine - 在本地使用 cloud_sql_proxy 获取 notAuthorized 错误

java - 动态寻找项目的目的地

java - Hashmap 是否自动排序?

java - 应用程序引擎。 HTTP错误: 503 on Gradle setup

python - Google App Engine 和 404 错误