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.

AppGameKit/AppGameKit Studio Showcase / [Code Tier1] XML read, write and parse

Author
Message
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 17th May 2015 23:02 Edited at: 20th May 2015 09:21
Hi,

I have written an XML parser because I thought it would be a good way to save data for games. It's a bit easier to read than a list of unidentified text or colon delimited text which is what I used to use.

There are limits:
The reader isn't exactly universal. The XML definition is far too broad for that. Whilst the reader may be able to read a random XML that you throw at it, it is better if you stick to reading XMLs that have been created with this. You can see from the attached sample XML what it can handle.

There is code to read an XML from file, write an XML to file and create an XML in memory ready to write to file.
I have included a sample XML and sample code.
The sample code reads the sample XML and writes it back again. It also creates an XML in memory and writes that to file.
The code is not very commented but there is a large comments section at the start which explains the functions that you need to use.

I have attached a rar file containing the main project, the XML include file and the test xml.
But for anyone that doesn't like downloading archives (You know who you are) then here are the three files:

Main.agk


Sample.xml



XML.agc


OOPS!
I forgot to include this file in the rar. You will need these additional string functions too:


AGK V2 user - Tier 1 (mostly)

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th May 2015 23:51
Very useful, thanks for sharing!

I'm using flat files for my AppGameKit Framework, this will be a neater solution. It means I can serialise my data which is a much faster way to load and save data.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 19th May 2015 11:18 Edited at: 20th May 2015 09:18
Limitations

1. Attribute lists can't be parents
2. You are limited to 3 nesting levels - including the root
3. There is a limit of 10 attributes per element
4. Single line elements and attribute lists that are children of the root must be written before the last multi-line element.

1. Attribute lists can't be parents.
This is valid xml:

But the code doesn't support attribute lists as parents. Instead I would suggest doing something like this:


2. You are limited to 3 nesting levels - including the root, like this:

You can't do this:


3. There is a limit of 10 attributes per element.


4. Single line elements and attribute lists that are children of the root must be written before the last multi-line element.
This is OK:

But this isn't:


Fixes/work-arounds:
1. It's a valid way to write XML but I haven't coded for it. I don't see it as a limitation though so unless anyone has a specific need for it then it will stay as it is.

2. I really should change this.
You can create as many nested levels as you like but the writer will get confused. So for now stick to three but watch this space for updates.

3. I don't really think this one is a problem. If you need more than 10 attributes per element then you might want to rethink the design of your XML.

4. This should be easy to fix in theory but I have written the write XML code as a recursive function and my head starts to fry when I try to follow the code - making debugging a nightmare! So for now just ensure that the last thing before you close the root is a multi-line element.

Also: The first line is only included so that it produces a valid XML. When the code reads the file, it skips that line completely but it is necessary if you want to use the XML file with some other software.

Finally:
The code is all there for you to use and tweak. The user functions are the only ones you need to call in order to use XML files but all of the other functions are there too. So if you want to make the changes yourself or create additional functionality then be my guest.

However, All these limitations are completely superfluous if you use the 'write' code to create the XML in the first place

AGK V2 user - Tier 1 (mostly)
JohnnyMeek
11
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 20th May 2015 17:42
This is really useful, thanks for sharing.
unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 20th May 2015 23:56
Very nice Scraggle. Thanks!

Using AppGameKit v2 T1 + T2
Systems: Primary: Mac OS X 10.10
Secondary: Windows 7
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th May 2015 00:16
Just experimenting with your commands, and found a minor bug in extractString. It doesn't work if the left string is longer than one character. here is the corrected version:



For example, retrieving the value of:

<MyValue>OptionA</MyValue>

by looking between <MyValue> and </MyValue> was returning:

MyValue>OptionA

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 28th May 2015 00:30
Good spot!

My XML code is identifying tag content by looking between ">" and "<" so I didn't notice this.

Thanks BatVink

AGK V2 user - Tier 1 (mostly)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th May 2015 01:28
I noticed it because I had to use the string functions instead of XML. I am currently reading an XML stream from a http response straight into a string. I don't think that is catered for in your XML functions, I would have to "unparse" it into elements and tags.

If my need gets any greater, I will add something to do this and post back here.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Login to post a reply

Server time is: 2024-11-24 19:40:45
Your offset time is: 2024-11-24 19:40:45