Java Reference
In-Depth Information
Listing 19.4 shows the source code of the DomainEditor application.
LISTING 19.4
The Full Text of DomainEditor.java
1: import java.io.*;
2: import nu.xom.*;
3:
4: public class DomainEditor {
5: public static void main(String[] arguments) throws IOException {
6: try {
7: // create a tree from the XML document feed.rss
8: Builder builder = new Builder();
9: File xmlFile = new File(“feed.rss”);
10: Document doc = builder.build(xmlFile);
11:
12: // get the root element <rss>
13: Element root = doc.getRootElement();
14:
15: // get its <channel> element
16: Element channel = root.getFirstChildElement(“channel”);
17:
18: // get its <link> elements
19: Elements children = channel.getChildElements();
20: for (int i = 0; i < children.size(); i++) {
21:
22: // get a <link> element
23: Element link = children.get(i);
24:
25: // get its text
26: Text linkText = (Text) link.getChild(0);
27:
28: // update any link matching a URL
29: if (linkText.getValue().equals(
30: “http://www.cadenhead.org/workbench/”)) {
31:
32: // update the link's text
33: link.removeChild(0);
34: link.appendChild(“http://www.cadenhead.org/”);
35: }
36: }
37:
38: // create new elements and attributes to add
39: Element item = new Element(“item”);
40: Element itemTitle = new Element(“title”);
41:
42: // add them to the <channel> element
43: itemTitle.appendChild(
44: “Fuzzy Zoeller Sues Over Libelous Wikipedia Page”
45: );
46: item.appendChild(itemTitle);
Search WWH ::




Custom Search