Java Reference
In-Depth Information
Evaluating XOM
These three sample applications cover the core features of the main XOM package and
are representative of its straightforward approach to XML processing.
There also are smaller nu.xom.canonical , nu.xom.converters , nu.xom.xinclude , and
nu.xom.xslt packages to support XInclude, XSLT, canonical XML serialization, and
conversions between the XOM model for XML and the one used by DOM and SAX.
Listing 19.7 contains an application that works with XML from a dynamic source: RSS
feeds of recently updated web content from the producer of the feed. The RssFilter
application searches the feed for specified text in headlines, producing a new XML docu-
ment that contains only the matching items and shorter indentation. It also modifies the
feed's title and adds an RSS 0.91 document type declaration if one is needed in an RSS
0.91 format feed.
LISTING 19.7
The Full Text of RssFilter.java
1: import nu.xom.*;
2:
3: public class RssFilter {
4: public static void main(String[] arguments) {
5:
6: if (arguments.length < 2) {
7: System.out.println(“Usage: java RssFilter rssFile searchTerm”);
8: System.exit(-1);
9: }
10:
11: // Save the RSS location and search term
12: String rssFile = arguments[0];
13: String searchTerm = arguments[1];
14:
15: try {
16: // Fill a tree with an RSS file's XML data
17: // The file can be local or something on the
18: // Web accessible via a URL.
19: Builder bob = new Builder();
20: Document doc = bob.build(rssFile);
21:
22: // Get the file's root element (<rss>)
23: Element rss = doc.getRootElement();
24:
25: // Get the element's version attribute
26: Attribute rssVersion = rss.getAttribute(“version”);
27: String version = rssVersion.getValue();
28:
29: // Add the DTD for RSS 0.91 feeds, if needed
30: if ( (version.equals(“0.91”)) & (doc.getDocType() == null) ) {
Search WWH ::




Custom Search