Reading and Writing XML files?

For questions about using Classic.

Post » Wed Sep 09, 2009 11:23 am

Is there a way to write and read XML files from a Construct application, and if so, what is the best way to go about this?

To my knowledge there is no XML plugin, but it seems like python script might provide a roundabout means of handling XML files.

I'm also considering building an XML add-on, as I believe I have all the necessary tools, although I'm not sure as of yet how much work would be involved.

Any advice is welcome. :)
B
21
S
13
G
8
Posts: 306
Reputation: 5,458

Post » Wed Sep 09, 2009 5:39 pm

I've made a couple of XML parsers in Construct -- made one just last night, actually. I don't actually have an example on hand but I can give the general idea around my parsers.

I usually use the ListBox object to load a file, then I use it's "For each line" event to read through the entire file. Inside the subevents to this event, you can read the file using tokens. To get the name of the tag on the current line, you'd this expression:

[code:1vng6jel]GetToken(GetToken(ListBox.LineText(ListBox.CurrentIndex), 2, "<"), 1, ">")[/code:1vng6jel]
And to get the contents of an element, you'd use this expression:

[code:1vng6jel]GetToken(GetToken(ListBox.LineText(ListBox.CurrentIndex), 2, ">"), 1, "<")[/code:1vng6jel]
Unfortunately, this only allows you to get the content of an element if your file is arranged like this:

[code:1vng6jel]<tag>stuff</tag>[/code:1vng6jel]
Instead of this:

[code:1vng6jel]<tag>
stuff
</tag>[/code:1vng6jel]
So, this method works, but it's pretty limited. Hope that helps some, anyways. :)
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692

Post » Wed Sep 09, 2009 7:46 pm

I don't think there's any standards-compliant way to read or write XML files in Construct at the moment - it would certainly make for a useful plugin.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Wed Sep 09, 2009 8:01 pm

Sounds like Python might be well suited for this.
Image Image
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Post » Mon Sep 14, 2009 1:38 am

Thanks for the information. It looks like your method Linkman2004 is the best way to handle simple XML using events. With a little more code, it might even be able to handle more complicated XML, and so I may end up using a solution like that.

That said, I'm curios as to whether I could use a third party python library for XML manipulation in Construct. I'm not overly familiar with the capabilities of Construct's python scripting system.

As an alternative I'm still considering building an XML plugin, as it might be the easiest way to get standards compliant XML manipulation, and I would just like to start building plugins in general. My current thinking is that if there is an open source XML library that is suitably simple to work with, I might be able to drop that into a plugin, and then add some simple action/condition/expression code to make its features accessible from Construct.

Granted, I've never built a plugin for Construct before, so if anyone has a recommendation for an XML library that might work, or an estimation as to how difficult the process of making it a plugin will be, I would appreciate the advice. Thanks again for the feedback. :D
B
21
S
13
G
8
Posts: 306
Reputation: 5,458

Post » Mon Sep 14, 2009 4:06 am

There's always TinyXML if you decide to make a plugin. It's small, open-source, and seems to be fairly popular.

I don't know how accurate my estimation is, but I can't imagine it would be too hard to make a plugin using it. Once one gets used to it, the SDK is actually really simple to work with and the difficulty level starts boiling down more to ones own coding skills.
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692

Post » Sat Sep 26, 2009 5:59 am

I'm fluent with parsers and have done some for XML before.
I considered suggesting this but I have no idea on how would one store the resulting data. Hash table? can't put another hash table inside of one so :s

Would have to be an XML object and you'd have to query it, could be a bit complex to use.
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests