简体   繁体   English

当我尝试使用xstream将xml响应转换为pojo时出现Java错误

[英]java- error when i am trying to convert xml response into pojo using xstream

I am getting an error for the following code in a java web app-- 我在Java Web应用程序中收到以下代码的错误-

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

The error is shown for the line of code just above this line-- error="Type mismatch- cannot convert from Object to apiresponse" 该行上方的代码行显示了错误-error =“ Type mismatch-无法从Object转换为apiresponse”

Given below is the XML that I have to parse--- 下面给出的是我必须解析的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>

I have created the following java classes to obtain data from above xml--- 我创建了以下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;
}


}

What am I doing wrong here? 我在这里做错了什么?

You have many errors, but the one corresponding to your message is you have to cast the result of xstream.fromXML to an apiresponse' object : 您有很多错误,但是与您的消息相对应的是您必须将xstream.fromXML的结果强制转换为apiresponse'对象:

apiresponse result = (apiresponse)xstream.fromXML(resp);

Moreover, the code you provided (the Java classes) do not compile, there are many errors. 此外,您提供的代码(Java类)无法编译,存在许多错误。

Here are some improvements : 以下是一些改进:

Result.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 : 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 : 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;
  }

} }

And finally your code to unmarshall the XML : 最后是解编XML的代码:

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

    ApiResponse result = (ApiResponse)xstream.fromXML(resp);

All this code is working fine with Xstream 1.4.2 所有这些代码都可以在Xstream 1.4.2上正常工作

Try to follow Sun's coding convention for your classes name, attributes names, etc... Use XstreamAliases to adapt the Java class name to the XML name. 尝试遵循Sun的类名称,属性名称等编码约定。使用XstreamAliases使Java类名称适应XML名称。

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

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