Java Tutorial - Java Script : Modifying an XML Document

Java Tutorial - Java Script : 

Modifying an XML Document

This for loop cycles through each child element of the channel element. Elements without names can be retrieved by calling their parent node’s getChild() method with one argument: an integer indicating the element’s position within the parent node:
Text linkText = (Text) link.getChild(0);
This statement creates the Text object for the text http://www.cadenhead.org/workbench/ found within the link element. Text elements always will be at position 0 within their enclosing parent.
To work with this text as a string, call the Text object’s getValue() method, as in this statement:
if (linkText.getValue().equals(“http://www.cadenhead.org/workbench/”))
// ...
}
The DomainEditor application only modifies a link element enclosing the text “http://www.cadenhead.org/workbench/”. The application makes the following changes:
The text of the link element is deleted, the new text “http://www.cadenhead.org/” is added in its place, and then a new item element is added. A parent node has two removeChild() methods to delete a child node from the document. Calling the method with an integer deletes the child at that position:
Element channel = domain.getFirstChildElement(“channel”);
Element link = dns.getFirstChildElement(“link”);
link.removeChild(0);
These statements would delete the Text object contained within the channel’s first link element. Calling the removeChild() method with a node as an argument deletes that particular node. Extending the previous example, the link element could be deleted with this statement:
channel.removeChild(link);
Listing 19.4 shows the source code of the DomainEditor application.
The DomainEditor application displays the modified XML document to standard output, so it can be run with the following command to produce a file named feeds2.rss:
java DomainEditor > feed2.rss