XML Parsing

Chris Pons cmpons at gmail.com
Mon Mar 19 20:06:11 PDT 2012


Hey Guys,
I am trying to parse an XML document with std.xml. I've looked 
over the reference of std.xml as well as the example but i'm 
still stuck. I've also looked over some example code, but it's a 
bit confusing and doesn't entirely help explain what i'm doing 
wrong.

As far as I understand it, I should load a file with read in 
std.file and save that into a string. From there, I check to make 
sure the string xmlData is in a proper xml format.

This is where it gets a bit confusing, I followed the example and 
created a new instance of the class document parser and then 
tried to parse an attribute from the start tag map. The value i'm 
targeting right now is the width of the map in tiles, and want to 
save this into an integer. However, the value I get is 0.

Any help would be MUCH appreciated.

Here is a reference to the XML file: http://pastebin.com/tpUU1Wtv


//These two functions are called in my main loop.
	void LoadMap(string filename)
	{
		enforce( filename != "" , "Filename is invalid!" );

		xmlData = cast(string) read(filename);

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

		debug StopWatch sw = StopWatch(AutoStart.yes);
		check(xmlData);
		debug writeln( "Verified XML in ", sw.peek.msecs, "ms.");		
	}
	
	void ParseMap()
	{
		auto xml = new DocumentParser(xmlData);

		xml.onStartTag["map"] = (ElementParser xml)
		{
			mapWidth = to!int(xml.tag.attr["width"]);
			xml.parse();
		};
		xml.parse();
		writeln("Map Width: ", mapWidth);
	}


More information about the Digitalmars-d-learn mailing list