Get a child node by name

Verified
Added by iNTERFACEWARE

Use node.child() to return the node of the specified name, works for all node types

Source Code
   -- create sample XML node
   local x = [[
   <patient SSN ="">
      <first-name>John</first-name>
      <last-name>Smith</last-name>
      <other-name>William</other-name>
      <other-name>Frederic</other-name>
   </patient>]]
   xmlMsg = xml.parse(x)
   trace(xmlMsg)
   
   -- get a named child node - in this case "first-name" and "last-name"
   local fname = xmlMsg.patient:child('first-name')
   local lname = xmlMsg.patient:child('last-name')
   
   -- get the 1st and 2nd "other-name"
   local oname1 = xmlMsg.patient:child('other-name')
   local oname2 = xmlMsg.patient:child('other-name',2)

Description
Use node.child() to return the node of the specified name, works for all node types
Usage Details

Use node.child() to return the node of the specified name from an XML node tree. If there are multiple children with the same name you can specify which one to retrieve (this is demonstrated in the code).

Note: The node.child() function works with all types of node trees.

How to use the snippet:

  • Paste the code into your script