0

他正在使用 sax 解析器,而不是列出我想要一次提取一条记录的所有内容。这是我从中提取的 xml

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
    <title>Conservation Tips</title>
    <link>blah</link>
    <description>Living comfortably during a Memphis summer can be challenging, but it   does not have to be costly. What are some of the easiest ways to stay cool and save?</description>
    <item>
        <title>Have a professional, reputable contractor clean and inspect your air conditioner. This should be done every year, whether you have window or central units.</title>
    </item>
    <item>
        <title>Set the thermostat at 78 degrees or higher for the most energy efficient operation. Each degree below this setting adds 6% to your cooling costs.</title>
    </item>
    <item>
        <title>Check your air conditioner's filter every time you receive your utility bill.</title>
        </item>
    <item>
        <title>Use fans to move the air inside your home. This gives the sensation that it is 5 degrees cooler than the actual temperature.</title>
        </item>
    <item>
        <title>Shade windows on the sunny side of your home.</title>
        </item>
    <item>
        <title>Keep drapes closed or add room-darkening shades to block out the heat from the sun.</title>
        </item>
    <item>
        <title>The outside portion of a central air conditioner is the condensing unit. Keep it clear from dried mud, debris and grass clippings, because it needs to breathe.</title>
        </item>
    <item>
        <title>Use your programmable thermostat to automatically increase the temperature setting at bedtime.</title>
        </item>
    <item>
        <title>Sleep under lightweight bedding and use fans during sleep.</title>
        </item>
    <item>
        <title>Do not place lamps near your thermostat. The thermostat senses the heat produced from the lamp and causes the air conditioner to run longer than necessary.</title>
        </item>
    <item>
        <title>Do not set your thermostat at a colder setting than normal when you turn on your air conditioner. It will not cool your home any faster and could result in excessive cooling and, therefore, unnecessary expense.</title>
        </item>
</channel>

这是我的代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    try {
        url = new URL("url");
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    RssFeed feed = null;
    try {
        feed = RssReader.read(url);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ArrayList<RssItem> rssItems = feed.getRssItems();
    for(RssItem rssItem : rssItems) {
        Log.i("RSS Reader", rssItem.getTitle());
        //Log.i(rssItem.getTitle(), rssItem.getDescription());
        //Log.i("RSS Content", rssItem.getLink());
    }
}}

我想做的是一次随机显示一个项目。我可以把它们都拉出来,但似乎不能一次得到一个。任何帮助表示赞赏。谢谢

4

2 回答 2

0

据我所知,您不能使用 SAX 解析器进行随机访问。您可以改用 DOM 解析器,它可以为您提供随机访问能力,或者您可以简单地获取所有项目,然后只处理随机选择的一项,即:

ArrayList<RssItem> rssItems = feed.getRssItems();

Random rand = new Random(System.currentTimeMillis());

//Pick one at random
Log.i("RSS Reader", rssItems.get(rand.nextInt(rssItems.size())).getTitle();
于 2012-04-27T14:54:48.807 回答
0

您可以将 RssItems 存储在从 1 到 numberOfRssItems 的键的映射中。然后使用随机数生成器生成一个介于 1 和 numberOfRssItems 之间的数字,并使用随机数作为键从映射中获取该记录。

于 2012-04-27T15:24:46.940 回答