Sample code for 30+ languages & platforms
Zig

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.

Note: This example requires Chilkat v9.5.0.76 or greater. The TagIndex method was introduced in v9.5.0.76

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

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

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("FatturaElettronicaHeader");
    xml.updateChildContent("DatiTrasmissione|IdTrasmittente|IdPaese", "IT");
    xml.updateChildContent("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>

    const x_t = try xml.findChild("DatiTrasmissione|IdTrasmittente");
    defer x_t.deinit();
    const index = x_t.tagIndex("IdPaese");
    if (index < 0) {
        std.debug.print("nothing found at the given tag.\n", .{});
        return;
    }

    const x1 = try x_t.newChildAfter(index, "NewTag", "Content");
    defer x1.deinit();

    // Show the resulting XML.
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});
}