## Registration code : 12 ##
It is currently Mon Nov 20, 2017 10:45 pm

All times are UTC + 1 hour [ DST ]

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: XML
PostPosted: Sun Jan 11, 2009 1:24 pm 
Site Admin
User avatar
 WWW  Profile

Joined: Mon Jan 28, 2008 9:44 pm
Posts: 200
Location: France
I've written a small (70kb of source code) XML parser, suited for the needs of the engine.
Why not using TinyXML anymore ? Well, I really liked TinyXML's simplicity, but I needed a validator.
I've looked at standard validation formats (DTD and XLS) but I didn't like the syntax and the apparent complexity.
So I decided to write my own parser and validator, and it was quite fun :) I think this is a good exercise for learning C++ and string manipulation (which is sooooooo easy with s_str, by the way).

This XML lib is not meant for every usage : it is tailored for games.
For example, I have absolutely no need for this :
<?xml version="1.0" encoding="ISO-8859-1"?>

So the parser wont read it (actually, this line will throw an error and the file will not be parsed).

As stated above, I'll make heavy use of validation. Here is a sample validation file (*.def, but can be anything you like) :

<d:Frame name inherits="" b:virtual="false" b:setAllPoints="false" b:hidden="false" n:alpha="1.0" parent="" b:toplevel="false" b:movable="false" b:resizable="false" frameStrata="PARENT" b:enableMouse="false" b:enableKeyboard="false">

        <n[*]:AbsDimension n:x n:y/>
        <n[*]:RelDimension n:x="1.0" n:y="1.0"/>
        <Anchor point="TOPLEFT" relativeTo="" relativePoint="TOPLEFT">
                <n[*]:AbsDimension n:x="0" n:y="0"/>
                <n[*]:RelDimension n:x="0.0" n:y="0.0"/>
        <n[*]:AbsInset n:left="0" n:right="0" n:top="0" n:bottom="0"/>
        <n[*]:RelInset n:left="0.0" n:right="0.0" n:top="0.0" n:bottom="0.0"/>




    <-- Includes -->

    <-- Include : Parses another XML file -->
    <Include file/>
    <-- Script : Parses a Lua file -->   
    <Script file/>
    <-- Objects -->

(the original file is much bigger, but I've extracted the essential)

Here is the documentation :
  • A block preceded by :
    • "d:" indicates definition of the pre-defined block.
    • "d[X]:" indicates this block inherits from "X" (it takes X's attributes and sub-block, and can add new ones).
    • "l:" indicates loading a pre-defined block.
    • "n[a,b]:" can only be present "b" times at most, and "a" times at least. If b=".", there is no upper limit, and if a=".", the block can be omitted.
    • "n[STAR]:" indicates a radio block : it can only be present if none of the other radio blocks on the same level are.
    • default : "n[.,.]:", no constraint
  • An attribute preceded by :
    • "s" will be typed as a string.
    • "n" will be typed as a number.
    • "b" will be typed as a boolean.
    • "nothing" will be typed as a string by default (string can contain everything)
  • An attribute followed by "=" and a value can be omitted and will take this value if omitted, just like C++ function parameters.

This file contain two parts :
  • Predefinitions :
    These are XML blocks at root level, and whose names are preceded by "d:" or "d[X]:".
    This is only a definition : just like a function you define in C++ won't be called unless you explicitly call it, you'll have to "invoke" (load) these pre-defined blocks in the file structure for them to be used.
    Note that you can also load pre-defined blocks inside other pre-defined blocks (see pre-definition of "Frame:Frames" for example : it contains a reference to "Frame", which is being declared).
  • File structure :
    The file structure tells the parser what kind of blocks it can expect in an XML file.
    It contains only one main block (here : "Ui"), and this main block can contain as many children (sub-blocks) as you want. You can also load pre-defined blocks using "l:".

So, in short, here is what this parser supports :
  • Unlimited number of children/attributes.
  • Smart comments : a block that starts with "<!" will be ignored (and all of its content too).
  • Smart comments : a block that starts with "<--" and ending by "-->" is a multi-line comment.
  • Basic content validation :
    • Inheritance.
    • Optional attributes (with default values).
    • Precise control over how many times a block can be present.
  • Attributes must be stored as strings, but can be typed as string, number or boolean.
  • Detailed errors/warnings with file+line+block reference.
  • Conditional parsing "<#[SOME_CONDITION]> ... </#>" : the content of this block will only be parsed if "SOME_CONDITION" is passed to the parser.
... and what it's not planned to support :
  • Non string based attributes.
  • Strongly typed attributes (for example : enums). These should be handled by the program, and not by the lib.
  • Multiple inheritance. You can make an inheritance chain ("Unit -> Creature -> Boss" for example), but a single Block can't inherit two or more other ones at once.

- Kal.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour [ DST ]

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
SourceForge.net Logo
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group  
Design By Poker Bandits