PageSourceSearch

https://www.rssboard.org/news/221/read-rss-feed-java-using-xom

html rssboard.org collected 2026-09-24 08:26:44 UTC 15,792 bytes, 252 lines download raw bytes

1<!DOCTYPE html
2PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
5<head>
6<!-- Google tag (gtag.js) -->
vendor: 65 bytes, lines 6-7
6
7<script async src="https://www.googletagmanager.com/gtag/js?id=
7G-J80V1HPMKT
vendor: 13 bytes, line 7
7"></script>
8<script>
9  
vendor: 138 bytes, lines 9-13
9window.dataLayer = window.dataLayer || [];
10  function gtag(){dataLayer.push(arguments);}
11  gtag('js', new Date());
12
13  gtag('config', '
13G-J80V1HPMKT
vendor: 5 bytes, line 13
13');
14</script>
14
15<title>How to Read an RSS Feed with Java Using XOM</title>
16<meta name="viewport" content="width=device-width, initial-scale=1">
17<link rel="alternate" type="application/rss+xml" title="RSS Advisory Board" href="http://feeds.rssboard.org/rssboard" />
18<link rel="preconnect" href="https://fonts.googleapis.com">
19<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
20<link href="https://fonts.googleapis.com/css2?family=Anton&family=Lisu+Bosa:wght@700&display=swap" rel="stylesheet">
21<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
22<style type="text/css" media="screen">
23@import url("https://www.rssboard.org/images/stylesheet.css");
24</style>
25<meta name="robots" content="noodp" /><meta name="description" content="  There are a lot of libraries for processing XML data with Java that can be used to read RSS feeds. One of the best is" /><link rel="canonical" href="https://www.rssboard.org/news/221/read-rss-feed-java-using-xom" />
26</head>
27<body>
28<!-- ukey="13C1F4FD" -->
29<!-- Begin Page -->
30<div class="container page">
31  <div class="row">
32  	<div class="col col-md-12 banner">
33		<h1 class="bannerhead"><a href="/">RSS Advisory Board</a></h1>
34		<h2 class="bannersubhead">Really Simple Syndication specifications, tutorials and discussion</h2>
35  	</div>
36  </div>
37  <div class="row">
38	<div class="col col-md-10"><!-- Begin Google AdSense -->
39<div align="center">
40<p>
40<script type="text/javascript"><!--
41google_ad_client = "pub-8378161688790357";
42/* RSS Board, 728x90, created 10/30/10 */
43google_ad_slot = "8997688149";
44google_ad_width = 728;
45google_ad_height = 90;
46//-->
47</script>
47
48<script type="text/javascript"
49src="//pagead2.googlesyndication.com/pagead/show_ads.js">
50</script>
50</p>
51	</div>
52<!-- End Google AdSense --><!-- Begin Main Content -->
53<!-- Begin Entries -->
54<div class="entries">
55
56
57
58<a name="221"></a><div class="entryhead"><p><a href="https://www.rssboard.org/news/221/read-rss-feed-java-using-xom">How to Read an RSS Feed with Java Using XOM</a></p></div><div class="entry"><figure class="text-center"><img src="https://www.rssboard.org/images/xml-in-a-nutshell-3rd-edition-cover-elliotte-rusty-harold-w-scott-means.jpg" alt="Cover of the O'Reilly book XML in a Nutshell, 3rd Edition, by Elliotte Rusty Harold and W. Scott Means. The cover features a black-and-white illustration of the neck and head of a peafowl bird with a crest of tipped feathers on his head." class="img-fluid img-thumbnail" width="350" /></figure>
59
60<p>There are a lot of libraries for processing XML data with Java that can be used to read RSS feeds. One of the best is the <a href="https://xom.nu/">open source library XOM</a> created by the computer book author Elliotte Rusty Harold.</p>
61
62<p>As he wrote one of his 20 books about Java and XML, Harold got so frustrated with the available Java libraries for XML that he created his own. XOM, which stands for XML Object Model, was designed to be easy to learn while still being strict about XML, requiring documents that are well-formed and utilize namespaces in complete adherence to the specification. (At the RSS Advisory Board, talk of following a spec is our love language.)</p>
63
64<p>XOM was introduced in 2002 and is currently up to version 1.3.9, though all versions have remained compatible since 1.0. To use XOM, download the class library in one of the packages available on the XOM homepage. You can avoid needing any further configuration by choosing one of the options that includes third-party JAR files in the download. This allows XOM to use an included SAX parser under the hood to process XML.</p>
65
66<p>Here's Java code that loads items from The Guardian's <a href="https://www.rssboard.org/rss-specification">RSS 2.0</a> feed containing articles by Ben Hammersley, displaying them as HTML output:</p>
67
68<p><code>
69// create an XML builder and load the feed using a URL<br />
70Builder bob = new Builder();<br />
71Document doc = bob.build("https://www.theguardian.com/profile/benhammersley/rss");<br />
72// load the root element and channel<br />
73Element rss = doc.getRootElement();<br />
74Element channel = rss.getFirstChildElement("channel");<br />
75// load all items in the channel<br />
76Elements items = channel.getChildElements("item");<br />
77for (Element item : items) {<br />
78&nbsp;&nbsp;// load elements of the item<br />
79&nbsp;&nbsp;String title = item.getFirstChildElement("title").getValue();<br />
80&nbsp;&nbsp;String author = item.getFirstChildElement("creator",<br />
81&nbsp;&nbsp;&nbsp;&nbsp;"http://purl.org/dc/elements/1.1/").getValue();<br />
82&nbsp;&nbsp;String description = item.getFirstChildElement("description").getValue();<br />
83&nbsp;&nbsp;// display the output<br />
84&nbsp;&nbsp;System.out.println("&lt;h2>" + title + "&lt;/h2>");<br />
85&nbsp;&nbsp;System.out.println("&lt;p>&lt;b>By " + author + "&lt;/b>&lt;/p>");<br />
86&nbsp;&nbsp;System.out.println("&lt;p>" + description + "&lt;/p>");<br />
87}
88</code></p>
89
90<p>All of the classes used in this code are in the top-level package <code>nu.xom</code>, which has comprehensive <a href="https://xom.nu/apidocs/">JavaDoc</a> describing their use. Like all Java code this is a little long-winded, but Harold's class names do a good job of explaining what they do. A Builder uses its <code>build()</code> method with a URL as the argument to load a feed into a Document over the web. There are also <a href="https://xom.nu/apidocs/nu/xom/Builder.html#build-java.io.File-">other build methods</a> to load a feed from a file, reader, input stream, or string.</p>
91
92<p>Elements can be retrieved by their names such as "title", "link" or "description". An element with only one child of a specific type can be retrieved using the <code>getFirstChildElement()</code> method with the name as the argument:</p>
93
94<p><code>Element linkElement = item.getFirstChildElement("link");</code></p>
95
96<p>An element containing multiple children of the same type uses <code>getChildElements()</code> instead:</p>
97
98<p>
99<code>Elements enclosures = item.getChildElements("enclosure");
100if (enclosures.size() > 1) {<br />
101&nbsp;&nbsp;System.out.println("I'm pretty sure an item should only include one enclosure");<br />
102}
103</code></p>
104
105<p>If an element is in a namespace, there must be a second argument providing the namespace URI. Like many RSS feeds, the ones from The Guardian use a <a href="https://www.rssboard.org/rss-profile#namespace-elements-dublin-creator">dc:creator</a> element from Dublin Core to credit the item's author. That namespace has the URI "http://purl.org/dc/elements/1.1/".</p>
106
107<p>If the element specified in <code>getFirstChildElement()</code> or <code>getChild Elements()</code> is not present, those methods return <code>null</code>. You may need to check for this when adapting the code to load other RSS feeds.</p>
108
109<p>If the name Ben Hammersley sounds familiar, he <a href="https://www.theguardian.com/media/2004/feb/12/broadcasting.digitalmedia">coined the term "podcasting"</a> in his February 2004 article for The Guardian about the new phenomenon of delivering audio files in RSS feeds.</p><a name="more"></a><div class="entryfooter">Posted by Rogers Cadenhead at 2023/08/01 11:25 PM | 1765 COMMENTS | <a href="https://www.rssboard.org/news/221/read-rss-feed-java-using-xom">permalink</a></div></div>
110
111<div class="dateheader">Comments</div><a name="discuss"></a>
112
113<div class="entry"><p>No comments yet.</p></div>
114<a name="reply"></a><h2>Add a Comment</h2>
115
116<p>All comments are moderated before publication. These HTML tags are permitted: &lt;p&gt;, &lt;b&gt;, &lt;i&gt;, &lt;a&gt;, and &lt;blockquote&gt;.</p>
117
118<form method="post" id="commentForm" action="https://www.rssboard.org/news/221/read-rss-feed-java-using-xom">
119    <div class="form-group">
120        <label for="author">Name</label>
121        <input type="text" class="form-control" name="author" placeholder="Enter your name" />
122    </div>
123    <div class="form-group">
124        <label for="link">Website</label>
125        <input type="text" class="form-control" name="link" placeholder="Enter your website address (or leave blank)" />
126    </div>
127    <div class="form-group">
128        <label for="comment">Comment</label>
129        <textarea class="form-control" name="comment" rows="10"></textarea>
130    </div>
131    <div class="text-center">
132        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
133    </div>
134</form></td></tr>
135</table>
136</form>
137<!-- End Entries -->
138
139</div><!-- End Main Content -->
140</div>
141<div class="col col-md-2 sidebar">
142<!-- Begin Sidebar -->
143
144<p class="text-center">
145	<a href="/"><img src="/images/rss-feed-icon-96-by-96.png" width="96" alt="RSS icon" class="img-fluid"></a>
146</p>
147
148<div class="sidebargroup">
149<div class="sidebartitle">Main Menu</div>
150<div class="sidebarlink"><a href="https://www.rssboard.org/">RSS Advisory Board</a></div>
151<div class="sidebarlink"><a href="https://www.rssboard.org/rss-specification">RSS Specification</a></div>
152<div class="sidebarlink"><a href="https://www.rssboard.org/rss-profile">RSS Profile</a></div>
153<div class="sidebarlink"><a href="https://www.rssboard.org/rss-validator/">RSS Validator</a></div>
154<div class="sidebarlink"><a href="https://www.rssboard.org/rss-autodiscovery">RSS Autodiscovery</a></div>
155<div class="sidebarlink"><a href="https://www.rssboard.org/advisory-board">Board Members</a></div>
156<div class="sidebarlink"><a href="https://www.rssboard.org/charter" rel="nofollow">Charter</a></div>
157<div class="sidebarlink"><a href="https://groups.google.com/g/rss-public">RSS-Public Mailing List</a></div>
158<div class="sidebarlink"><a href="https://www.rssboard.org/rss-language-codes">RSS Language Codes</a></div>
159<div class="sidebarlink"><a href="https://www.rssboard.org/rsscloud-interface">RSSCloud Interface</a></div>
160<div class="sidebarlink"><a href="https://www.rssboard.org/rss-history">RSS History</a></div>
161</div>
162
163<div class="sidebargroup">
164<div class="sidebartitle">RSS Feeds</div>
165<div class="sidebarrsslink"><a href="http://feeds.rssboard.org/rssboard" rel=\"nofollow\"><img src="/images/rss-icon.png" alt="Subscribe to Really Simple Syndication" border="0" width="16" height="16" /></a> <a href="http://feeds.rssboard.org/rssboard" rel=\"nofollow\">This Weblog</a></div>
166</div>
167
168<div class="sidebargroup">
169<div class="sidebartitle">Social Media</div>
170<div class="sidebarlink"><a rel="me" href="https://mastodon.social/@rssboard">Mastodon</a></div>
171</div>
172
173<div class="sidebargroup">
174<div class="sidebartitle">Spec Library</div>
175<div class="sidebarlink"><a href="https://www.rssboard.org/rss-specification">RSS 2.0 (Current)</a></div><div class="sidebarlink"><a href="https://www.rssboard.org/rss-2-0">RSS 2.0</a></div><div class="sidebarlink"><a href="https://www.rssboard.org/rss-0-9-2">RSS 0.92</a></div><div class="sidebarlink"><a href="https://www.rssboard.org/rss-0-9-1">RSS 0.91</a> (UserLand)</div><div class="sidebarlink"><a href="https://www.rssboard.org/rss-0-9-1-netscape">RSS 0.91</a> (Netscape)</div><div class="sidebarlink"><a href="https://www.rssboard.org/rss-0-9-0">RSS 0.90</a></div>
176<div class="sidebarlink"><a href="https://www.rssboard.org/rss-change-notes">RSS Change Notes</a></div>
177</div>
178
179<div class="sidebargroup">
180<div class="sidebartitle">Namespaces</div>
181<div class="sidebarlink"><a href="https://www.rssboard.org/creative-commons">
181Creative Commons</a></div>
182<div class="sidebarlink"><a href="https://www.rssboard.org/media-rss">Media RSS</a></div>
183<div class="sidebarlink"><a href="https://www.rssboard.org/trackback">Trackback</a></div>
184</div>
185
186<div class="sidebargroup">
187<div class="sidebartitle">Archive</div>
188<div class="sidebarlink"><a href="https://www.rssboard.org/site-index" rel="nofollow">Site Index</a></div>
189</div>
190
191<div class="sidebargroup">
192<div class="sidebartitle">Board Members</div>
193<div class="sidebarlink">Rogers Cadenhead</div>
194<div class="sidebarlink">Sterling "Chip" Camden</div>
195<div class="sidebarlink">Simone Carletti</div>
196<div class="sidebarlink">James Holderness</div>
197<div class="sidebarlink">Jenny Levine</div>
198<div class="sidebarlink">Eric Lunt</div>
199<div class="sidebarlink">Randy Charles Morin</div>
200<div class="sidebarlink">Ryan Parman</div>
201<div class="sidebarlink">Jake Savin</div>
202<div class="sidebarlink">Jason Shellen</div>
203<div class="sidebarlink">Paul Querna</div>
204</div>
205<div class="sidebargroup">
206<div class="sidebartitle">Other Formats</div>
207<div class="sidebarlink"><a href="https://datatracker.ietf.org/doc/html/rfc4287" rel="nofollow">Atom</a></div>
208<div class="sidebarlink"><a href="http://web.resource.org/rss/1.0/" rel="nofollow">RDF Site Summary</a></div>
209</div>
210
211
212
213<!-- End Sidebar -->
214</div>
215</div>        <div class="row top10header">
216			<div class="col">
217				<h2 class="top10">Popular Pages on This Site</h2>
218			</div>
219		</div>
220		<div class="row">
221			<div class="col">                
222				<ul>
223					<li><a href="https://www.rssboard.org/rss-validator/">RSS Validator</a></li>
224                	<li><a href="https://www.rssboard.org/media-rss">Media RSS Specification (Current)</a></li>
225                	<li><a href="https://www.rssboard.org/rss-specification">RSS 2.0 Specification</a></li>
226                	<li><a href="https://www.rssboard.org/rss-profile">RSS Best Practices Profile</a></li>
227					<li><a href="https://www.rssboard.org/redirect-rss-feed">How to Redirect an RSS Feed</a></li>
228                </ul>
229            </div>
230            <div class="col">
231            	<ul>
232                	<li><a href="https://www.rssboard.org/rss-language-codes">RSS Language Codes</a></li>
233                	<li><a href="https://www.rssboard.org/rss-enclosures-use-case">Payloads for RSS</a></li>
234                	<li><a href="https://www.rssboard.org/rss-encoding-examples">RSS Encoding Examples</a></li>
235                	<li><a href="https://www.rssboard.org/rss-autodiscovery">RSS Autodiscovery</a></li>
236                	<li><a href="https://www.rssboard.org/news/212/read-rss-feed-php-using">How to Read an RSS Feed with PHP Using SimplePie</a></li>
237                </ul>
238            </div>
239        </div></div>
240<!-- End Page -->
241<div class="footer">
242<p><a href="/">RSS Advisory Board</a> |
243<a href="http://feeds.rssboard.org/rssboard">RSS Feed</a> |
244<a href="https://www.rssboard.org/rss-specification">RSS Specification</a> |
245Hosted by <a href="https://workbench.cadenhead.org/">Workbench</a></p>
246</div>
247</div>
248<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
248
249<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
249
250<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
250
251</body>
252</html>

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.