Unicode C
Unicode C
Insert XML into existing XML by calling InsertChildTreeAfter
See more XML Examples
Demonstrates how to insert an XML tree or subtree into another XML document using the InsertChildTreeAfter method.Chilkat Unicode C Downloads
#include <C_CkXmlW.h>
void ChilkatSample(void)
{
HCkXmlW xml;
HCkXmlW xml2;
int index;
// Build the 1st XML document:
// <x1>
// <a1>1</a1>
// <a2>2</a2>
// <a3>3</a3>
// </x1>
xml = CkXmlW_Create();
CkXmlW_putTag(xml,L"x1");
CkXmlW_UpdateChildContent(xml,L"a1",L"1");
CkXmlW_UpdateChildContent(xml,L"a2",L"2");
CkXmlW_UpdateChildContent(xml,L"a3",L"3");
// Build a 2nd XML document:
// <x2>
// <b1>11</b1>
// <b2>22</b2>
// <b3>33</b3>
// </x2>
xml2 = CkXmlW_Create();
CkXmlW_putTag(xml2,L"x2");
CkXmlW_UpdateChildContent(xml2,L"b1",L"11");
CkXmlW_UpdateChildContent(xml2,L"b2",L"22");
CkXmlW_UpdateChildContent(xml2,L"b3",L"33");
// We want to insert xml2 into xml to get this:
// <x1>
// <a1>1</a1>
// <x2>
// <b1>11</b1>
// <b2>22</b2>
// <b3>33</b3>
// </x2>
// <a2>2</a2>
// <a3>3</a3>
// </x1>
// Insert xml2 after "a1"
index = CkXmlW_TagIndex(xml,L"a1");
if (index < 0) {
wprintf(L"a1 not found.\n");
CkXmlW_Dispose(xml);
CkXmlW_Dispose(xml2);
return;
}
CkXmlW_InsertChildTreeAfter(xml,index,xml2);
wprintf(L"%s\n",CkXmlW_getXml(xml));
// The result is:
// <x1>
// <a1>1</a1>
// <x2>
// <b1>11</b1>
// <b2>22</b2>
// <b3>33</b3>
// </x2>
// <a2>2</a2>
// <a3>3</a3>
// </x1>
// Note: xml2 now points to the subtree within xml.
// For example, update "b3" and then see how it's updated within the merged document:
CkXmlW_UpdateChildContent(xml2,L"b3",L"33333333");
wprintf(L"%s\n",CkXmlW_getXml(xml));
// The result:
// <x1>
// <a1>1</a1>
// <x2>
// <b1>11</b1>
// <b2>22</b2>
// <b3>33333333</b3>
// </x2>
// <a2>2</a2>
// <a3>3</a3>
// </x1>
CkXmlW_Dispose(xml);
CkXmlW_Dispose(xml2);
}