Java Reference
In-Depth Information
LISTING 19.7
Continued
31: DocType rssDtd = new DocType(“rss”,
32: “http://my.netscape.com/publish/formats/rss-0.91.dtd”);
33: doc.insertChild(rssDtd, 0);
34: }
35:
36: // Get the first (and only) <channel> element
37: Element channel = rss.getFirstChildElement(“channel”);
38:
39: // Get its <title> element
40: Element title = channel.getFirstChildElement(“title”);
41: Text titleText = (Text)title.getChild(0);
42:
43: // Change the title to reflect the search term
44: titleText.setValue(titleText.getValue() + “: Search for “ +
45: searchTerm + “ articles”);
46:
47: // Get all of the <item> elements and loop through them
48: Elements items = channel.getChildElements(“item”);
49: for (int i = 0; i < items.size(); i++) {
50: // Get an <item> element
51: Element item = items.get(i);
52:
53: // Look for a <title> element inside it
54: Element itemTitle = item.getFirstChildElement(“title”);
55:
56: // If found, look for its contents
57: if (itemTitle != null) {
58: Text itemTitleText = (Text) itemTitle.getChild(0);
59:
60: // If the search text is not found in the item,
61: // delete it from the tree
62: if (itemTitleText.toString().indexOf(searchTerm) == -1)
63: channel.removeChild(item);
64: }
65: }
66:
67: // Display the results with a serializer
68: Serializer output = new Serializer(System.out);
69: output.setIndent(2);
70: output.write(doc);
71: } catch (Exception exc) {
72: System.out.println(“Error: “ + exc.getMessage());
73: exc.printStackTrace();
74: }
75: }
76: }
19
 
Search WWH ::




Custom Search