Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

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

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

let xml = chilkat::Xml::new();
xml.set_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>

let x_t = xml.find_child("DatiTrasmissione|IdTrasmittente").unwrap();
let index = x_t.tag_index("IdPaese");
if index < 0 {
    println!("nothing found at the given tag.");
    return;
}

let x1 = x_t.new_child_after(index, "NewTag", "Content").unwrap();

// Show the resulting XML.
println!("{}", xml.get_xml().unwrap_or_default());