Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML in D

Tags:

xml

d

I'm trying to parse a XML document that stores data for a map in my 2D game. I'm trying to take this step by step, I've loaded the file, create a new document parser, and picked a start tag and attribute I want to pull. However when I check to see what the value of the attribute should be (25) it comes out as zero. Telling me that i'm missing something and it's not pulling the XML value.

This is the XML file being parsed: http://pastebin.com/tpUU1Wtv

    void LoadMap(string filename)
{
    enforce( filename != "" , "Filename is invalid!" );

    xmlData = cast(string) read(filename);

    enforce( xmlData != "", "Read file Failed!" );

}

void ParseMap()
{
    auto xml = new DocumentParser(xmlData);

    xml.onStartTag["map"] = (ElementParser e)
    {
        mapWidth = to!int(e.tag.attr["width"]);
    };
    xml.parse();
    writeln("Map Width: ", mapWidth);
}
like image 445
RedShft Avatar asked Feb 20 '26 19:02

RedShft


1 Answers

The current xml module seems to be a bit buggy, an alternative is being worked on, I believe.

The reason your code doesn't work is that for some reason the parser ignores the outer, enclosing tag. Which in your case is "map". If you wrap your map tag in a dummy tag, then suddenly it does work.

<dummy>
   <map...>
    ...
   </map>
</dummy>
like image 152
fwend Avatar answered Feb 22 '26 15:02

fwend