Java Reference
In-Depth Information
* @return The object as a String.
*/
public String toString()
{
StringBuilder str = new StringBuilder();
Set<String> set = attributes.keySet();
for (String item : set)
{
str.append(item);
str.append('=');
str.append(attributes.get(item));
str.append('\n');
}
str.append("Items:\n");
for (RSSItem item : items)
{
str.append(item.toString());
str.append('\n');
}
return str.toString();
}
}
It is very easy to use the
RSS
class. Simply call the
load
method, and pass an
InputStream
that contains RSS data. Once you have loaded the RSS data, you can
call the
getAttributes
function of the RSS class to access any attributes of the
<channel>
element. Additionally, you can call the
getItems
function to gain access
to the
RSSItems
that were parsed.
This chapter includes two recipes that demonstrate exactly how to use the
RSS
class
to parse RSS data. In the next several sections, you will see how the RSS parsing class was
constructed.
Loading the RSS File
Calling the
load
method of the
RSS
class will load RSS data at the specified URL. The
first thing that the
load
method does, is to open a connection and obtain an
InputStream
to the URL that contains the RSS data.
URLConnection http = url.openConnection();
InputStream is = http.getInputStream();
Next, the DOM is setup to parse the XML data from the
InputStream
.
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
Document d = factory.newDocumentBuilder().parse(is);