Navigating XML in LiveCode

Ken Ray kray at sonsothunder.com
Mon Jan 23 13:15:13 EST 2012


On Jan 23, 2012, at 10:34 AM, Graham Samuel wrote:

> Pete - thanks for your reply (the only one so far - are there no XML users on the list??).

Sorry, Graham… was a bit busy before to respond. 

> I notice that people tend to use employee lists and the like; but so far I haven't found a very clear explanation of how you get attributes into new nodes at a stroke, which is my problem. (In fact I'm not really doing an employee database, but the concept is very similar.)

You can't do that in one go; you'd need to use three commands/functions:

revInsertXMLNode (or revAddXMLNode)  -- to create a new XML node in the tree
revXMLNumberOfChildren  -- to count the number of "employee" nodes there are either before or after you create the new node
revSetXMLAttribute  -- to set the attribute on the new node you created.

> I suppose more generally I'm trying to understand if XML is suitable for this kind of data structure, where there are several nodes identical or similar in structure but containing different data. It looks ideal to me, but the difficulty of finding the last item you added, and of adding attributes at creation time, is giving me doubts. But maybe I'm just confused.

So suppose you had a simple XML tree like this that was in the variable "tMyXML":

<root>
   <employees>
        <employee firstName="Ken" lastName="Ray"/>
   </employees>
</root>

and you want to add a new employee. 

If you're using the revXML external, you'd first load the XML and parse it and get a 'tree ID', which is the indicator for the XML document you're using:

  put revCreateXMLTree(tMyXML,false,true,false) into tTreeID

Next, you want to use that tree to add a node, like this:

  put "/root/employees" into tRootNode  -- makes it easier to work with
  put revXMLNumberOfChildren(tTreeID, tRootNode,"employee") into tNodeCount  -- which is "1" in our case
  revInsertXMLNode tTreeID, (tRootNode & "/employee[" & tNodeCount & "]"),"employee","","after"
  -- Now the new node is added as "employee[2]" (which is the same as "tNodeCount+1")
  put tNodeCount+1 into tNewNum
  -- Set the attributes:
  revSetXMLAttribute tTreeID,(tRootNode & "/employee[" & tNewNum & "]"),"firstName","Graham"
  revSetXMLAttribute tTreeID,(tRootNode & "/employee[" & tNewNum & "]"),"lastName","Samuel"

If you're using my script-only stsXMLLibrary, you'd do this:

  put "/root/employees" into tRootNode  -- makes it easier to work with
  put stsXML_LoadData(tMyXML) into tDocID
  put stsXML_AppendChild(tDocID & tRootNode,"ELEM","employee") into tNewNodeID
  get stsXML_SetAttribute(tNewNodeID,"firstName","Graham")
  get stsXML_SetAttribute(tNewNodeID,"lastName","Samuel")

It's a bit of a pain, but that's XML for you… and don't forget to add error checking after each command/function call just to make sure your XML isn't returning errors.


Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/	




More information about the use-livecode mailing list