Swift
Swift
XML Add Child Tree
Demonstrates the Xml.AddChildTree method.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Build the following XML:
// <?xml version="1.0" encoding="utf-8"?>
// <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
// <soap12:Body>
// <cteDadosMsg xmlns="http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4">
// </cteDadosMsg>
// </soap12:Body>
// </soap12:Envelope>
let xml = CkoXml()!
xml.tag = "soap12:Envelope"
xml.addAttribute(name: "xmlns:xsi", value: "http://www.w3.org/2001/XMLSchema-instance")
xml.addAttribute(name: "xmlns:xsd", value: "http://www.w3.org/2001/XMLSchema")
xml.addAttribute(name: "xmlns:soap12", value: "http://www.w3.org/2003/05/soap-envelope")
xml.updateAttrAt(tagPath: "soap12:Body|cteDadosMsg", autoCreate: true, attrName: "xmlns", attrValue: "http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4")
// We want to add the following XML as a sub-tree under the "cteDadosMsg" element.
// <consStatServCTe versao="4.00" xmlns="http://www.portalfiscal.inf.br/cte">
// <tpAmb>2</tpAmb>
// <cUF>43</cUF>
// <xServ>STATUS</xServ>
// </consStatServCTe>
// Build the XML to be added as a child tree.
let xml2 = CkoXml()!
xml2.tag = "consStatServCTe"
xml2.addAttribute(name: "versao", value: "4.00")
xml2.addAttribute(name: "xmlns", value: "http://www.portalfiscal.inf.br/cte")
xml2.updateChildContent(tagPath: "tpAmb", value: "2")
xml2.updateChildContent(tagPath: "cUF", value: "43")
xml2.updateChildContent(tagPath: "xServ", value: "STATUS")
// Add the child tree at the desired location:
// Temporarily point to the location where we wish to add the child tree.
success = xml.findChild2(tagPath: "soap12:Body|cteDadosMsg")
if success == false {
// Restore current location to the root.
xml.getRoot2()
return
}
success = xml.addChildTree(tree: xml2)
// Restore the current location to the root.
xml.getRoot2()
// You can see now that the XML contain the child tree:
print("\(xml.getXml()!)")
// <?xml version="1.0" encoding="utf-8"?>
// <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
// <soap12:Body>
// <cteDadosMsg xmlns="http://www.portalfiscal.inf.br/cte/wsdl/CTeStatusServicoV4">
// <consStatServCTe versao="4.00" xmlns="http://www.portalfiscal.inf.br/cte">
// <tpAmb>2</tpAmb>
// <cUF>43</cUF>
// <xServ>STATUS</xServ>
// </consStatServCTe>
// </cteDadosMsg>
// </soap12:Body>
// </soap12:Envelope>
}