Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] XML parser v1.1

Author
Message
Phaelax
DBPro Master
16
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jan 2011 19:26 Edited at: 11th Jan 2011 00:46
A year ago, I released the first version of my xml parser. I recently had an actual need for it, so I made a few modifications and packaged it up to where I feel it is finally production ready. Here's a list of the available functions:

xmlReadFile(string)
xmlGetTagName$(integer)
xmlGetAttributeValue$(integer, string)
xmlGetElementContent$(integer, boolean)
xmlClear()

I wouldn't call this a 100% complete XML package, but it should be enough to get the job done. The earlier version was still unable to extract attributes, this one does. As I can't really use arrays inside a UDT, the attributes are only exacted from a tag when you attempt to retrieve it. Because the attributes are not stored independently anywhere internally, you should store this value yourself when you call it so the text parsing doesn't have to be used more than necessary.

I've attached a sample XML file to be used in this example. The output of this example is specific to using this XML file, but you should see how easy it is to use. I plan to add a few other functions for retrieving parent and child nodes. It's technically possible now, but the simplified functions haven't been implemented yet.

Edit: Jan. 8, 2011
Updated to version 1.2 which fixes a bug that stored leading spaces and tab characters in an element's content. This only became visible when using Cloggy's DLL which obeys the tabs in strings, whereas DB's default text/print commands ignore them.

Edit: Jan 10, 2011
Updated to 1.21, which fixed a bug in the xmlGetAttributeValue() function that creeps up in Windows 7 and possibly Vista.



"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein

Attachments

Login to view attachments
Lost Dragon
8
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 27th Jan 2011 00:03 Edited at: 27th Jan 2011 00:04
This looks interesting and I want to use it a little later.

I found a mappy script/plugin that will give Mappy the ability to export map files (with layers) to xml.

Phaelax's code could probably be used to read it but I didn't understand it well enough to make it work when I tried the first time. I'm going to try it again later, but for now I'm just using a plain text csv. If anyone else wants to give it a shot here are the tools:

Mappy
http://www.tilemap.co.uk/mappy.php

Script/plugin
http://flixel.org/forums/index.php?topic=211.0

If I get something to work I'll post it here later.
Frank C
8
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 27th Jan 2011 14:11
I'm not using Phaelax's code for XML I use a plugin from the useful TGC plugins page in Program Announcements forum. I downloaded mappy and this script and will take a look at how it works also.
Phaelax
DBPro Master
16
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 16th Feb 2011 03:55 Edited at: 16th Feb 2011 03:55
I took a look at the example XML. I tried running it, but my code gets an error with that particular xml file, I'm not sure why yet.

But to parse it, I'd have to make a slight modification to the existing code to add newline character to the tag elements content (for the LAYER tag).

But more or less this is how it might look:


As for the "objects" tag, the example only showed a single child element and considering its name was "OBJ1" makes me think additional elements would be sequentially ordered; OBJ2, OBJ3, OBJ4
The only way to read those in without knowing how many could be there would be to look for any tag that starts with OBJ but does not equal OBJECTS and just assume its one of the numbered object tags. A better solution for whoever wrote the xml exporter would be to alter it like so:


"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
WLGfx
11
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 16th Feb 2011 16:02
I've been keeping up with this and I've had a quick look around on xml files. It'd be quite interesting having something like this in a scene editor I'm currently working on. I take it, this code will read and write to xml files?

I'm building a scene editor for levels. ie, load a map, place objects and assign data to them, place spawn areas, and floating platforms. I was thinking about just writing the data out as just raw data but in this format it can be tweaked by the user. My project won't be a modeler just a scene editor (ie put prefabs in place)

Warning! May contain Nuts!
Phaelax
DBPro Master
16
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 16th Feb 2011 21:31
It only reads xml files, doesn't write them.

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Lost Dragon
8
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 16th Feb 2011 21:52
I've been working with different map editors trying to figure out how I can save myself some time by using tools that are already made.

You might take a look at Tiled. It has a "tmx" format which is basically a renamed xml file as far as I can tell.

Tiled.

It doesn't need special scripts to output tmx files and the editor is open source.

I'll take a look at what you said about loading. I am currently stuck trying to figure out how to handle objects.
Phaelax
DBPro Master
16
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Feb 2011 20:18
The data in that xml file however is encoded in Base64 then uses gzip compression. I have DB code for the Base64 but it would still need decompressed. Even then, you only get an array of numbers and I haven't found anything yet that explains what they mean.

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Lost Dragon
8
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 17th Feb 2011 21:24
Oh I'm sorry, I forgot to mention this.

In Tiled select edit->preferences and then check "store tile layer data as csv".

That will output the map array in a familiar format.
WLGfx
11
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 17th Feb 2011 21:45
Nice map editor by the way 'Lost Dragon'. I suppose xml files are really used in non-speed essential file handling. Probably the same with csv files. Using xml files maybe handy for scene graphs in 3d apps so the user can simply search for a particular entry and alter a number. I'm looking at saving mine out as text files as well as raw data files, like .x files. (text and binary options)

Warning! May contain Nuts!
Frank C
8
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 3rd Mar 2011 13:27
Quote: "The data in that xml file however is encoded in Base64 then uses gzip compression. I have DB code for the Base64 but it would still need decompressed. Even then, you only get an array of numbers and I haven't found anything yet that explains what they mean."


Actually Phaelax the data that comes from the tiled program does not need to be encoded in Base64 and compressed, that is just the default option, I use it with settings that leave it in standard xml format non encoded and not compressed. The layer element which contains the array of numbers is the image numbers for the tiles in your tileset. I have outputted the xml using CSV format but am having a hard time getting them to be read into DBP if I use more than a 10*10 grid.
Lost Dragon
8
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 3rd Mar 2011 22:31
Frank, you were able to load a 10x10 grid from a TMX file? Can you show what you have in the main development forum? I'd really like to be able to load and use TMX files. Maybe if enough people work on it then it can be figured out.
Frank C
8
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 4th Mar 2011 14:23
Lost Dragon I'm still working out the issues with reading the data layer in properly and consuming it in my app. I haven't had much time since working on a high priority project at work which has included some weekend work. I will post my solution once I'm complete, I found that Tiled can save the grid data in either a CSV format or in individual XML elements which makes for a very long XML file if your map is large.

Login to post a reply

Server time is: 2019-06-20 00:46:26
Your offset time is: 2019-06-20 00:46:26