Hammer of Code

My adventures in GSoC 2008 working on Thousand Parsec, among other things

07 2008

xml and the like

Contrary to what I expected I ended up spending the day looking into XML, xml-parsers and map importing. I kicked things off by familiarizing myself with XML; turns out I had a decent grasp on how XML worked already. I did, however, learn that it was possible to define a “Document Type Definition” or DTD to specify which tags have what, which attributes exist of tags, and the like. I threw together a simple DTD for the map metalanguage; It allows the map to have multiple constellations, each with a name attribute. Those constellations can then have multiple planets. Each planet has a name, id, x, y attribute, as well as any number of “id”s of adjacent planets.

Here is the DTD for those versed in XML:

!DOCTYPE MAP [
!ELEMENT MAP (CNST*)>
!ELEMENT CNST (PLNT*)>
    !ATTLIST CNST name CDATA #REQUIRED>
!ELEMENT PLNT (ID*)>
    !ATTLIST PLNT name CDATA #REQUIRED>
    !ATTLIST PLNT id CDATA #REQUIRED>
    !ATTLIST PLNT x CDATA #REQUIRED>
    !ATTLIST PLNT y CDATA #REQUIRED>
!ELEMENT ID (#PCDATA)>
]>

(I had to remove the beginning braces because of display issues)

The only issue a predefined DTD raises is that it creates the need for a more sophisticated xml parser. My original intention was to use the TinyXml parser, but in its frugality it omits DTD validation of XML. DTD is a feature I think is fairly important for a map import class, primarily because a validated XML doc should THEORETICALLY contain a valid map (if my DTD does the job correctly.) That valid XML could then be imported via DOM, possibly directly, into the game. While a DOM import would be nice, it may not be possible. My backup plan would be the following:

  • Do a pass over all constellations. Create each one (using name), and add a pointer to it to a set.
  • Do a pass through each constellation, over all planets. Create each planet (using name, x, y and parent constellation) and map the specified id to the planets TRUE in-game ID.
  • Do another pass. This time adding adjacencies from the parsed XML to the game. This can be accomplished by using the ID map to relate xml IDs to true IDs.
  • Done. At this point a valid map should have been created

Going back to choosing an XML parser; My next candidate to investigate, and the #1 result when you search for “XML C++ Parser” is the Xerces C++ Parser. From what I understand, Xerces is a little bit more robust XML parser. While TinyXML is under a fairly loose license, Xerces is under the Apache General License, so I’m not entirely sure about its usability.

In other news: I just received an EZFLASH 3-in-1 GBA cart for my Nintendo DS, and so I will be bidding you all adieu to muck around with all the GBA homebrew (and other stuff) I can!


2 Responses to “xml and the like”

  1. While DTD is probably enough for your XML vocabulary, you may want to consider using XML Schema instead, mainly for two reasons: First, it is more expressive (thought also more complex) which allows you to more precisely describe your vocabulary (so you don’t need to perform extra validation in the application). Second, you can use tools to auto-generate C classes for your vocabulary along with parsing and serialization code. This way you don’t need to worry about XML parsers and converting text to/from C types. One such open-source (GPL proprietary license) tool is CodeSynthesis XSD:

    http://www.codesynthesis.com/products/xsd/

    If what I said above doesn’t make much sense, just take a look at the Hello World example in the getting started guide:

    http://www.codesynthesis.com/projects/xsd/documentation/cxx/tree/guide/#2

    Also, speaking of Xerces-C , Apache license is very permissive and a lot of business use software under this license in commercial, closed-source projects.

  2. Just was thinking about this post, and some earlier plans of mine.

    I would like to see what you come up with. It is the sort of thing that I would probably pull into the core for all rulesets to use, if it can be made general enough.

    Later
    Lee

Leave a Reply

« canada day Weekly Report 8 »