XML technologies / XML / XML in a Nutshell / The Document Object Model (DOM) / Documents with DOM: parsing

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

Although DOM Level 2 does not specify a proper interface for parsing a document, most implementations provide a simple interface for this purpose. It accepts a reference to an XML file, a stream or a URI. Once this interface has successfully parsed and validated a document (validation applies only to validating parsers), it usually provides a mechanism for obtaining a reference to an instance of the Document interface for the parsed document. The following code fragment shows how to parse a document with the Apache Xerces XML DOM parser:

// Erzeuge einen neuen Parser
DOMParser dp = new DOMParser(  );

// Parse das Dokument, und hole das DOM-Interface Document
dp.parse("http://www.w3.org/TR/2000/REC-xml-20001006.xml");
Document doc = dp.getDocument(  );

Note: DOM Level 3 introduces a standard mechanism for loading XML documents and reconstructing (saving) XML from DOM trees. JAXP also provides a standardized approach to these processes in Java, although the approaches of JAXP and DOM Level 3 differ.

  

 

 

 

back to top