Tutorial Downloads

File Size
XMLParsing-r99.capx4KB

Translations

Know another language? Translate this tutorial!

Stats

1,934 visitors
17.1K page views

XML Parsing

Favourite 33 favourites
Tutorial written by blackhornetOriginally published on 22nd, July 2012 - 4 revisions

This is just a very quick tutorial on parsing some XML text. I had to visit all three links referenced from the manual to figure this out, and thought a concrete example would help the next person.


I've borrowed this snippet from one of those sample sites (XPATH example). I'm pasting this just as a string, so note the double quotes:

    <?xml version=""1.0"" encoding=""utf-8""?>
    <Catalog>
      <Album artist=""The Last Shadow Puppets"" title=""The Age Of The Understatement"">
        <Track rating=""4"" length=""P3M7S"">The Age Of The Understatement</Track>
        <Track rating=""3"" length=""P2M18S"">Standing Next To Me</Track>
        <Track rating=""5"" length=""P2M26S"">Calm Like You</Track>
        <Track rating=""3"" length=""P3M38S"">Separate and Ever Deadly</Track>
        <Track rating=""2"" length=""P2M37S"">The Chamber</Track>
        <Track rating=""3"" length=""P2M44S"">Only The Truth</Track>
      </Album>
      <Album artist=""Kings Of Leon"" title=""Because Of The Times"">
        <Track rating=""4"" length=""P7M10S"">Knocked Up</Track>
        <Track rating=""2"" length=""P2M57S"">Charmer</Track>
        <Track rating=""3"" length=""P3M21S"">On Call</Track>
        <Track rating=""4"" length=""P3M09S"">McFearless</Track>
        <Track rating=""1"" length=""P3M59S"">Black Thumbnail</Track>
      </Album>
    </Catalog>

First load the string with the XML:Load action.

To get started parsing, you need a For each node "/Catalog", to parse the Catalog node. In a sub-event you now parse the Album nodes, and from another sub-event you parse the Track nodes.

That traverses the tree. Now at each level you add the actions to get at the data via the XML.StringValue() expression. Attributes require an @ prefix (so: @rating), and the node data itself can use the tag node().

You can also get the number of nodes with the XML.NodeCount() expression, with the relevant name.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Comments

0
Ragevortex 5,796 rep

Excelent tutorial. Short and to the point you just saved me hours of fighting with endless loops. Thank you.

Monday, August 13, 2012 at 5:51:15 PM
-2
xeed 4,977 rep

This tutorial does not clear up how to load an XML File.

Friday, August 17, 2012 at 3:49:36 AM
4
Kyatric 69.3k rep

Comment by xeed

This tutorial does not clear up how to load an XML File.
@xeed: Of course it doesn't, the point of the tutorial is to parse XML not to load the file.
You can get your XML string thanks to the AJAX plugin (http://www.scirra.com/manual/107/ajax).
You might also want to check how to use project files in construct 2 (http://www.scirra.com/tutorials/328/using-project-files-in-construct-2)

Friday, August 17, 2012 at 5:37:02 AM
1
theames 3,028 rep

This all works but when you export as Windows 8 and run it on w8 as an app via visual studio The for each node never runs. I've tried with and without a / prefix in the node name too. Any ideas?

Tuesday, October 23, 2012 at 2:57:04 PM
0
Shapter 2,176 rep

Be careful that the XML object is not supported by CoccoonJS and Intel XDK.
I'm not sure about the others.

Monday, June 24, 2013 at 1:22:24 PM
1
braingame 1,513 rep

thanx for this tiny but informative tutorial.
mentioning "Attributes require an @ prefix" saved me a lot of time.

Wednesday, February 26, 2014 at 12:40:11 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.