简体   繁体   English

如何使用@XStream 为 RSS 提要显示 xml?

[英]How to use @XStream to display xml for RSS feeds?

I am trying to add the below code in java using Xstream annotations.我正在尝试使用 Xstream 注释在 java 中添加以下代码。 I completely don't understand how should I do it.我完全不明白我该怎么做。 Currently, I have a class named Channel.java目前,我有一个名为 Channel.java 的类

@XStreamAlias("channel")
public class Channel {

    private String link;

    @XStreamAlias("atom:link")
    private AtomLink atom_link;

    private String title;
           
    private String description;

    @XStreamImplicit
    private List<Item> itemList;

    // etc
}   

Image.java图像.java

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("image")
public class Image {
         
    private String URL;

    public String getUrl() {
        return URL;
    }

    public void setUrl(String url) {
        this.url = URL;
    }
}

I am trying to get the below code in the Channel class.我正在尝试在 Channel 类中获取以下代码。 So basically I'm trying to add a subelement in for RSS feeds.所以基本上我正在尝试为 RSS 提要添加一个子元素。

<image>
    <url>
        Url here
    </url> 
</image>

Expected XML预期的 XML

<channel>
    <title>
    </title>
                 
    <image>
        <url>
        </url>
    </image>

    <item>
    </item>
</channel>

How do I do this?我该怎么做呢? I have Image.java.我有 Image.java。 But how do I get it in Channel.java?但是如何在 Channel.java 中获取它?

check this检查这个

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("channel")
public class Channel {

    private String link;

    @XStreamAlias("atom:link")
    private String atom_link;

    private String title;
    private String description;

    private Image image;

     public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getAtom_link() {
        return atom_link;
    }

    public void setAtom_link(String atom_link) {
        this.atom_link = atom_link;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public List<String> getItemList() {
        return itemList;
    }

    public void setItemList(List<String> itemList) {
        this.itemList = itemList;
    }

    @XStreamImplicit 
     private List<String> itemList;

}

Image.java图像.java

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("image")
public class Image {
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

Tester.java测试程序

import com.thoughtworks.xstream.XStream;


public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Channel channel=new Channel();
        Image image=new Image();
        image.setUrl("http:://google.com");
        channel.setAtom_link("atomlink");
        channel.setImage(image);
        channel.setDescription("desc");
         XStream xstream = new XStream();
         xstream.alias("channel", Channel.class);

         System.out.println(xstream.toXML(channel));


    }

}

output:-输出:-

<channel>
  <atom__link>atomlink</atom__link>
  <description>desc</description>
  <image>
    <url>http:://google.com</url>
  </image>
</channel>

check this... this may not be the exact answer but u can get something from here..If i get your Expected XML file exactely ..i can provide u better solution检查这个......这可能不是确切的答案,但你可以从这里得到一些东西......如果我得到你的预期XML文件......我可以为你提供更好的解决方案

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

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