Insert an XML node

Verified
Added by iNTERFACEWARE

Use node.insert() to insert a node into an XML node tree

Source Code
   -- create sample XML node
   local x = [[
   <patient SSN ="">
      <first-name>John</first-name>
      <last-name>Smith</last-name>
   </patient>]]
   xmlMsg = xml.parse(x)
   trace(xmlMsg)
   
   -- insert an ATTRIBUTE - put id before SSN 
   xmlMsg.patient:insert(1,xml.ATTRIBUTE, 'id')
   trace(xmlMsg)
   
   -- insert an ELEMENT  - put middle name in the middle
   xmlMsg.patient:insert(4, xml.ELEMENT, 'middle-name')
   trace(xmlMsg)
   
   -- insert an TEXT element - before the names
   xmlMsg.patient:insert(3,xml.TEXT, 'Insert comment before names')
   trace(xmlMsg)
   
   -- insert a CDATA (unparsed text) element - before the names again
   xmlMsg.patient:insert(4, xml.CDATA, 'Because CDATA is not parsed it allows & and <>')
   trace(xmlMsg)
Description
Use node.insert() to insert a node into an XML node tree
Usage Details

Use node.insert() to insert a new XML node into an XML node tree. The code shows how to insert the four different types of XML nodes. There are a few tricks to learn with XML node trees, so be sure to look the “more info” links below.

Note: The node.insert() function only works with XML node trees.

How to use the snippet:

  • Paste the desired insert code into your script