使用 Jsoup 解析嵌套的 rss (XML)

Parsing nested rss (XML) with Jsoup

我正在尝试编写一个服务,它将转到 rss feed 并下载每个条目对象并从中创建一个 java 对象。我遇到问题的地方是 ListItem 创建。我不知道如何访问 entry 标签内的 XML 标签。我认为它应该如何工作是在 for 循环中,但 Element e 似乎没有将内部 XML 识别为字段。

元素对象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>

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

对象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
}

您可以将选择器与 | 字符一起使用:

package com.github.davidepastore.Whosebug35304577;

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;

/**
 * Whosebug 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();
        }
    }
}