XML Parsing

1

Attached Files

The following files have been attached to this tutorial:

.capx

xmlparsing-r99.capx

Download now 4.31 KB

Stats

5,375 visits, 8,831 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 22 Jul, 2012. Last updated 19 Feb, 2019

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.

.CAPX

xmlparsing-r99.capx

Download now 4.31 KB
  • 0 Comments

Want to leave a comment? Login or Register an account!