StillLearningToCode
StillLearningToCode

Reputation: 2461

Parsing nested rss (XML) with Jsoup

I am trying to write a service that will go to rss feed and download each entry object and create a java object from it. Where I am having problems is in the ListItem creation. I don't know how to access XML tags inside the entry tag. How I think it should work is in the for loop, but the Element e doesn't seem to recognize the inner XML as fields.

Element object XML

<entry>
    <updated>2016-02-09T13:56:09-07:00</updated>
    <id im:id="389801252" im:bundleId="com.burbn.instagram">https://itunes.apple.com/us/app/instagram/id389801252?mt=8&amp;uo=2</id>
    <title>Instagram - Instagram, Inc.</title>
    <summary>...</summary>
    <im:name>Instagram</im:name>
    <link rel="alternate" type="text/html" href="https://itunes.apple.com/us/app/instagram/id389801252?mt=8&amp;uo=2"/>
    <im:contentType term="Application" label="Application"/>
    <category im:id="6008" term="Photo &amp; Video" scheme="https://itunes.apple.com/us/genre/ios-photo-video/id6008?mt=8&amp;uo=2" label="Photo &amp; Video"/>
    <im:artist href="https://itunes.apple.com/us/developer/instagram-inc./id389801255?mt=8&amp;uo=2">Instagram, Inc.</im:artist>
    <im:price amount="0.00000" currency="USD">Get</im:price>
    <im:image height="53">http://is3.mzstatic.com/image/thumb/Purple69/v4/a4/02/d2/a402d238-c5d2-1237-07cb-c6ff48cc1483/mzl.vpjstbmw.png/53x53bb-85.png</im:image>
    <im:image height="75">http://is5.mzstatic.com/image/thumb/Purple69/v4/a4/02/d2/a402d238-c5d2-1237-07cb-c6ff48cc1483/mzl.vpjstbmw.png/75x75bb-85.png</im:image>
    <im:image height="100">http://is4.mzstatic.com/image/thumb/Purple69/v4/a4/02/d2/a402d238-c5d2-1237-07cb-c6ff48cc1483/mzl.vpjstbmw.png/100x100bb-85.png</im:image>
    <rights>&#169; 2015 Instagram, LLC.</rights>
    <im:releaseDate label="October 6, 2010">2010-10-06T01:12:41-07:00</im:releaseDate>
    <content type="html">...</content>
</entry>

Code for downloading/parsing

@Override
    protected String doInBackground(String... params) {
        List<Element> elements = new ArrayList<>();
        try {
            Document doc = Jsoup.connect("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml").get();
            for (Element e : doc.select("entry")){
                elements.add(e);
                ListItem item = new ListItem(e.updated, e.artist, e.releaseDate);
                //rest of the code to add the new item to a data store
            }
            System.out.println("Number of entries: " + elements.size());

        } catch (IOException e) {
            e.printStackTrace();
        }

Object class:

public class ListItem {

  private String name;
  private String artist;
  private String releaseDate;

  public ListItem(String name, String artist, String releaseDate) {
    this.name = name;
    this.artist = artist;
    this.releaseDate = releaseDate;
  }

  //getters and setters omitted
}

Upvotes: 0

Views: 799

Answers (1)

Davide Pastore
Davide Pastore

Reputation: 8738

You can use selector with the | character:

package com.github.davidepastore.stackoverflow35304577;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * Stackoverflow 35304577 question.
 *
 */
public class App {
    public static void main( String[] args )
    {
        List<Element> elements = new ArrayList<Element>();
        try {
            Document doc = Jsoup.connect("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=10/xml").get();
            for (Element e : doc.select("entry")){
                elements.add(e);
                String updated = e.select( "updated" ).text();
                String artist = e.select( "im|artist" ).text();
                String releaseDate = e.select( "im|releasedate" ).text();
                System.out.println(updated + " - " + artist + " - " + releaseDate);
                ListItem item = new ListItem(updated, artist, releaseDate);
                //rest of the code to add the new item to a data store
            }
            System.out.println("Number of entries: " + elements.size());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Related Questions