Parsing a Document with DOM
Although DOM Level 2 doesn’t specify an actual interface
for parsing a document, most implementations provide a simple parsing
interface that accepts a reference to an XML document file, stream, or
URI. After this interface successfully parses and validates the
document (if it is a validating parser), it generally provides a
mechanism for getting a reference to the Document interface’s instance for the parsed
document. The following code fragment shows how to parse a document
using the Apache Xerces XML DOM parser:
// create a new parser
DOMParser dp = new DOMParser( );
// parse the document and get the DOM Document interface
dp.parse("http://www.w3.org/TR/2000/REC-xml-20001006.xml");
Document doc = dp.getDocument( );Tip
DOM Level 3 adds standard mechanisms for loading XML documents and reserializing (saving) DOM trees as XML. JAXP also provides standardized approaches for these processes in Java, although JAXP and DOM Level 3 offer different approaches.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access