02.06.2013 Views

XML Demystified

XML Demystified

XML Demystified

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1 <strong>XML</strong>: An Inside Look<br />

For example, you’d use a parser to retrieve book information from the books<br />

<strong>XML</strong> document that you saw earlier in this chapter. The extracted book information<br />

is then combined with HTML code to create a dynamic web page that displays the<br />

information about the book on the screen.<br />

Developers use one of two basic parsers. These are the Document Object Model<br />

(DOM) parser and the Simple API for <strong>XML</strong> (SAX).<br />

DOM reads the entire <strong>XML</strong> document into memory and then creates a tree<br />

structure of elements (see Figure 1-1). Various techniques are used to traverse the<br />

tree to local information contained in the <strong>XML</strong> document. You can also use DOM<br />

to write data to the <strong>XML</strong> document, but it’s limited to working with small <strong>XML</strong><br />

documents because the entire <strong>XML</strong> document is placed in memory.<br />

SAX reads the <strong>XML</strong> document, noting the locations of markup tags. The SAX<br />

parser makes a one-time pass through the <strong>XML</strong> from start to finish. As it encounters<br />

tags and data, it calls events that you define in your code. SAX is ideal for reading<br />

large <strong>XML</strong> documents because there aren’t any memory constraints; only a chunk<br />

of the <strong>XML</strong> document is ever in memory at one time. A drawback of SAX is that it<br />

cannot traverse an <strong>XML</strong> document. That is, SAX makes one pass through the<br />

document. If you want to return to a previous part of the document, then the<br />

document needs to be read from the beginning of the documents.<br />

Both DOM and SAX validate the contents of an <strong>XML</strong> document against a DTD.<br />

You’ll learn more about DOM and SAX later in this book.<br />

Figure 1-1 DOM transforms elements of an <strong>XML</strong> document into a tree structure,<br />

enabling the parser to traverse elements.<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!