Java Reference
In-Depth Information
You could
parse()
a document using the
DocumentBuilder
object,
builder
, like this:
File xmlFile = new File("D:/Beg Java Stuff/Address.xml");
Document xmlDoc = null;
try {
xmlDoc = builder.parse(xmlFile);
}
catch(SAXException e) {
e.printStackTrace();
System.exit(1);
}
catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
This code fragement requires imports for the
File
and
IOException
classes in the
java.io
package
as well as the
org.w3c.dom.Document
class name. We can now call methods for the
xmlDoc
object
to navigate through the elements in the document tree structure. Let's look at what the possibilities are.
Navigating a Document Object Tree
The
Node
interface that is defined in the
org.w3c.dom
package is fundamental to all objects that
encapsulate components of an XML document, and this includes the
Document
object itself. The
subinterfaces of
Node
that identify components of a document are:
Element
Represents an XML element.
Attr
Represents an attribute for an element.
Text
Represents text that is part of element content. This interface is
a subinterface of
CharacterData
, which in turn is a
subinterface of
Node
. References of type
Text
will therefore
have methods from all three interfaces.
CDATASection
Represents a CDATA section - unparsed character data.
Comment
Represents a document comment. This interface also extends
the
CharacterData
interface.
DocumentType
Represents the contents of a
DOCTYPE
declaration.
Entity
Represents an entity that may be parsed or unparsed.
EntityReference
Represents a reference to an entity.
Notation
Represents a notation declared in the DTD for a document. A
notation is a definition of an unparsed entity type.
ProcessingInstruction
Represents a processing instruction for an application.