Java Reference
In-Depth Information
position 0. There's a get() method to retrieve each element; call it with the integer posi-
tion of the element to be retrieved:
for (int i = 0; i < children.size(); i++) { Element link = children.get(i);
}
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/work-
bench/” 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.
19
A parent node has two removeChild() methods to delete a child node from the docu-
ment. 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);
Search WWH ::




Custom Search