Java Reference
In-Depth Information
The appendChild() method appends a new child below all other children of that parent.
The preceding statements produce this XML fragment:
<channel>
<link>http://www.cadenhead.org/workbench/</link>
</channel>
The appendChild() method also can be called with a String argument instead of a
node. A Text object representing the string is created and added to the element:
link.appendChild(“ http://www.cadenhead.org/workbench/” );
After a tree has been created and filled with nodes, it can be displayed by calling the
Document method toXML() , which returns the complete and well-formed XML document
as a String .
Listing 19.3 shows the complete application.
LISTING 19.3
The Full text of RssStarter.java
1: import nu.xom.*;
2:
3: public class RssStarter {
4: public static void main(String[] arguments) {
5: // create an <rss> element to serve as the document's root
6: Element rss = new Element(“rss”);
7:
8: // add a version attribute to the element
9: Attribute version = new Attribute(“version”, “2.0”);
10: rss.addAttribute(version);
11: // create a <channel> element and make it a child of <rss>
12: Element channel = new Element(“channel”);
13: rss.appendChild(channel);
14: // create the channel's <title>
15: Element title = new Element(“title”);
16: Text titleText = new Text(“Workbench”);
17: title.appendChild(titleText);
18: channel.appendChild(title);
19: // create the channel's <link>
20: Element link = new Element(“link”);
21: Text linkText = new Text(“http://www.cadenhead.org/workbench/”);
22: link.appendChild(linkText);
23: channel.appendChild(link);
24:
25: // create a new document with <rss> as the root element
26: Document doc = new Document(rss);
27:
28: // Display the XML document
Search WWH ::




Custom Search