Game Development Reference
In-Depth Information
the buzzword “AJAX” in most client/server web applications. Unfortunately, even
now, game development tools haven't quite made full use of XML.
12.2.1 Parsing Approaches
XML can be parsed in a variety of ways:
as a stream in a forward-reading fashion (e.g., SAX),
as a tree that can be traversed (e.g., DOM),
as a transform to convert data to another format (e.g., XSLT, XQuery),
as an object in memory (e.g., data binding, serialization).
I will summarize these various approaches; however, it is the final approach that
we will spend the rest of our time focusing on in this chapter.
Stream parsing. The first approach is quite general and a common one for some
developers when they first encounter XML. When reading a stream, each token
is parsed sequentially, and state is maintained throughout the parsing process. A
common implementation of stream parsing is the simple API for XML, or SAX.
While lacking a formal specification, it is still defined as an event-driven API where
events are raised for nodes, processing instructions, and comments [SAX 10]. Pro-
gramming against a SAX API would involve implementing callbacks for each of the
events of interest. SAX and stream approaches in general are known for using a
minimal amount of memory and being cache friendly. Drawbacks include being a
forward-only reader and not having access to the full document, which is needed
for some uses, such as XML validation [SAX 10].
Tree traversal. The second approach is most familiar to web programmers, as
this is the way you interface with HTML, XHTML, and XML from within the
browser. In general, it requires loading the full document into a tree structure in
memory that can be navigated. A common implementation is what is referred to as
the document object model, or DOM, which was standardized by the W3C in late
1998 [DOM 10]. Programming against a DOM involves walking nodes as siblings,
children, or parents and inspecting attributes and contained text. A DOM approach
allows for random access and is particularly well suited for XML validation. The
drawback to using a DOM approach is the memory usage that is required for really
large XML documents.
Transforms. The third approach is more of a utility approach: extracting and
transforming XML data. In this approach, an XML document is either queried or
converted to another format, which can be XML or entirely different. A common
implementation for transforming XML data is extensible stylesheet language trans-
formations, or XSLT. XSLT is a declarative language that itself is represented in
Search WWH ::




Custom Search