XML (Extensible Markup Language) is a document markup language that is aslo used for representing data structures, like when interacting with web services.
LiveCode has a number of built in functions for parsing and interacting with XML documents.
This tutorial will look at loading my blogs RSS feed (RSS feeds are XML documents) and then extracting the latest posts to display.
1. Get the XML document
get URL "http://activethought.net/feed/" put it into myXML
2. Load the XML into a XML tree
put revCreateXMLTree(myXML, false, true, false) into tTree
If you want to perform an error check (always a good thing to do) you can do so like so:
if tTree is not an integer then // error code goes here end if
3. Now that we have a XML tree, we can query it and extract content we need
// we can directly access nodes put "<h1>" put revXMLNodeContents(tTree, "rss/channel/title") && "-" && revXMLNodeContents(tTree, "rss/channel/description") -- this will out put my sites title and description put "</h1>"
Hint: If the XML node has any attributes set, we can access them with revXMLAttribute
e.g: put revXMLAttribute(tTree, “rss/channel/item/guid”, “isPermaLink”)
4. Extract all the recent posts
First we have to extract the nodes we we want to loop through. In this example, rss/channel contains my latest 10 posts (item)
put revXMLChildNames(tTree, "rss/channel/", return, "item", true) into recentPosts
We can now loop through these child nodes and display the post title and the link to the post
repeat for each line aPost in recentPosts put revXMLNodeContents(tTree, "rss/channel/" & aPost & "/title") into postTitle put revXMLNodeContents(tTree, "rss/channel/" & aPost & "/link") into postLink
Hint: If you want to find the number of child nodes use: revXMLNumberOfChildren
e.g. put “The last” && revXMLNumberOfChildren(tTree,”rss/channel/”,”item”, -1) && “posts are:”
And there we have it, a brief example on how to load an xml document and retrieve different values aswell as looping through a list of child nodes.