Game Development Reference
In-Depth Information
the form of an XML document [XSL 10] and can transform data from one schema
to another schema. XQuery, on the other hand, is a query language used to ex-
tract and transform data, which is commonly compared to SQL, which is a query
language for relational databases [XQu 10]. Capabilities of both of these languages
overlap; however, the purposes and language verbosity are entirely different.
Data binding. The final approach and the one I find is underused in game tool de-
velopment is XML data binding. Data binding involves mapping an XML schema
to a class definition. Rather than handling the parsing process directly, a pro-
grammer provides the class with or without attributes to handle deserializing the
XML data. The object can then be modified in memory and serialized back to
XML. The benefits of XML data binding include the ability to serialize across pro-
grams, languages, and platforms [Dat 10]. Additionally, a programmer doesn't have
to maintain parsing code to match the XML schema. Some drawbacks with data
binding are that not all data can be serialized properly (e.g., links to other objects),
and some data is not preserved (e.g., comments). However, as you'll see later in
this chapter, the benefits outweigh the drawbacks for the majority of the use cases
in tool development that can be aided by the use of data binding.
12.3 Parsing XML
Before we take a look at data binding, let's look at how we'd parse an XML docu-
ment via a DOM approach. If you're a seasoned developer, then you have probably
written code like this to manually parse an XML document. It's an easy first ap-
proach because you hop right in to coding up a mapping between your fields and
the incoming XML document.
The task usually comes in the form of either a sample XML document being
handed to you by your producer or from the server team or a designer. You're asked
to hook it up to the game. It is possible you could go back to the source, which
may be a database and query it directly; however, that is not always the case. You
may be consuming data from another application or pulling it down from a web
service.
Let us take an example of a designer who laid out some character stats in an Ex-
cel spreadsheet and exported that to XML for you to use in the game. Listing 12.1
is the document:
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas -microsoft-com:office:office"
xmlns:x="urn:schemas -microsoft-com:office:excel"
xmlns:html=" http: // www . w3 . org / TR / REC - html40 "
xmlns:ss="urn:schemas -microsoft-com:office:spreadsheet">
<DocumentProperties
xmlns="urn:schemas -microsoft-com:office:office">
<Author>Amir Ebrahimi</Author>
Search WWH ::




Custom Search