Sample code for 30+ languages & platforms
Android™

XML Add Child Tree

Demonstrates the Xml.AddChildTree method.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = 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>

    CkXml xml = new CkXml();
    xml.put_Tag("soap12:Envelope");
    xml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    xml.AddAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema");
    xml.AddAttribute("xmlns:soap12","http://www.w3.org/2003/05/soap-envelope");
    xml.UpdateAttrAt("soap12:Body|cteDadosMsg",true,"xmlns","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.

    CkXml xml2 = new CkXml();
    xml2.put_Tag("consStatServCTe");
    xml2.AddAttribute("versao","4.00");
    xml2.AddAttribute("xmlns","http://www.portalfiscal.inf.br/cte");
    xml2.UpdateChildContent("tpAmb","2");
    xml2.UpdateChildContent("cUF","43");
    xml2.UpdateChildContent("xServ","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("soap12:Body|cteDadosMsg");
    if (success == false) {
        // Restore current location to the root.
        xml.GetRoot2();
        return;
        }

    success = xml.AddChildTree(xml2);
    // Restore the current location to the root.
    xml.GetRoot2();

    // You can see now that the XML contain the child tree:

    Log.i(TAG, 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>

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}