Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.76+

Insert after Node with Tag

See more XML Examples

Demonstrates how to insert a new node as a sibling directly after a specified node having a given tag.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- First, let's create the following XML:

--   <FatturaElettronicaHeader>
--     <DatiTrasmissione>
--       <IdTrasmittente>
--         <IdPaese>IT</IdPaese>
--         <IdCodice>12345678</IdCodice>
--       </IdTrasmittente>
--     </DatiTrasmissione>
--   </FatturaElettronicaHeader>

local xml = chilkat.Xml.new()
xml.tag = "FatturaElettronicaHeader"
xml:update_child_content("DatiTrasmissione|IdTrasmittente|IdPaese", "IT")
xml:update_child_content("DatiTrasmissione|IdTrasmittente|IdCodice", "12345678")

-- Now insert a new node to get this XML:

--   <FatturaElettronicaHeader>
--     <DatiTrasmissione>
--       <IdTrasmittente>
--         <IdPaese>IT</IdPaese>
--         <NewTag>Content</NewTag>
--         <IdCodice>12345678</IdCodice>
--       </IdTrasmittente>
--     </DatiTrasmissione>
--   </FatturaElettronicaHeader>

local xT = xml:find_child("DatiTrasmissione|IdTrasmittente")
local index = xT:tag_index("IdPaese")
if index < 0 then
    print("nothing found at the given tag.")
    return
end

local x1 = xT:new_child_after(index, "NewTag", "Content")

-- Show the resulting XML.
print(xml:get_xml())