Search code examples
javaxmlxstreamfeed

How to use @XStream to display xml for RSS feeds?


I am trying to add the below code in java using Xstream annotations. I completely don't understand how should I do it. Currently, I have a class named 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

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. So basically I'm trying to add a subelement in for RSS feeds.

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

Expected XML

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

    <item>
    </item>
</channel>

How do I do this? I have Image.java. But how do I get it in Channel.java?


Solution

  • 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

    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