Java Reference
In-Depth Information
The
Builder()
and
Builder(true)
constructors both use the default parser—most likely
a version of Xerces. The presence of the Boolean argument
true
in the second construc-
tor configures the parser to be validating. It would be nonvalidating otherwise. A validat-
ing parser throws a
nu.xom.ValidityException
if the XML document doesn't validate
according to the rules of its document type definition.
The
Builder
object's
build()
method loads an XML document from a source and
returns a
Document
object:
Builder builder = new Builder();
File xmlFile = new File(“feed.rss”);
Document doc = builder.build(xmlFile);
These statements load an XML document from the file
feed.rss
barring one of two
problems: A
nu.xom.ParseException
is thrown if the file does not contain well-formed
XML, and a
java.io.IOException
is thrown if the input operation fails.
Elements are retrieved from the tree by calling a method of their parent node.
A
Document
object's
getRootElement()
method returns the root element of the docu-
ment:
Element root = doc.getRootElement();
In the XML document
feed.rss
, the root element is
domains
.
Elements with names can be retrieved by calling their parent node's
getFirstChildElement()
method with the name as a
String
argument:
Element channel = root.getFirstChildElement(“channel”);
This statement retrieves the
channel
element contained in the
rss
element (or
null
if
that element could not be found). Like other examples, this is simplified by the lack of a
namespace in the document; there are also methods where a name and namespace are
arguments.
When several elements within a parent have the same name, the parent node's
getChildElements()
method can be used instead:
Elements children = channel.getChildElements()
The
getChildElements()
method returns an
Elements
object containing each of the ele-
ments. This object is a read-only list and does not change automatically if the parent
node's contents change after
getChildElements()
is called.
Elements
has a
size()
method containing an integer count of the elements it holds. This
can be used in a loop to cycle through each element in turn beginning with the one at