Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component

Index of Chilkat Blog Posts

November 10, 2005

Creating Nodes with Chilkat XML in C++

I send you this mail since I didn’t find any answer in the sample code. I am creating a simple XML document in C++ that looks like this:

xml.put_Standalone( _T("yes") );
xml.put_Tag( _T("audit") );

// Create the first node
CkXml *node = xml.NewChild( _T("computer"), _T("") );
node->AddAttribute( _T("name"), _T("mycomputer") );
node->AddAttribute( _T("type"), _T("Windows") );
node->AddAttribute( _T("os"), _T("Microsoft Windows XP Pro") );

delete node;

// Create the the second node
CkXml *node = xml.NewChild( _T("disk"), _T("") );
node->AddAttribute( _T("name"), _T("C:") );
node->AddAttribute( _T("type"), _T("FAT") );
node->AddAttribute( _T("size"), _T("80000000″) );

delete node;

// Save…
xml.SaveXml( _T("c:\\test.xml") );

// Free the xml document < --- This entire block of code is unnecessary, see answers below...
CkXml *node = xml.FirstChild();
while ( node )
delete node;

node = xml.FirstChild();

The questions:

1) CREATING NODES: Is this the correct way to add new nodes to the xml document: using NewChild, adding attributes, and using delete node; ? If I do not delete the node here I get a memory leak. Something tells me that this (delete node) decreases some kind of reference counter?

2) DELETING ALL NODES: Any better or easier way to delete the whole xml document (free the nodes and release memory)?


1) Yes, that is the correct way to add new nodes. If you do not need to do anything with the
node being created, you may call xml.NewChild2(tag,content) which does not return anything.
In your case, you wish to add attributes, so calling NewChild is correct because you want
to call AddAttribute on the newly created child.
Any CkXml object pointers returned by a CkXml method call must be deleted. Each represents
a reference count to the entire XML tree. When the last reference is gone, the entire tree is
automatically deleted from memory. As long as one reference to any node in a tree exists,
the entire tree remains in memory. You can always get to the root node of a tree from
any other node by calling xml.GetRoot.

2) You can delete the entire section of code under the comment "Free the xml document".
It is not necessary. When the CkXml object goes out of scope, the last reference to the tree
is gone and the entire tree is deleted from memory.

PS> I have been doing quite a bit of programming myself using Chilkat XML, and these new methods will soon be available:

void RemoveChildWithContent(const char *content);
bool HasChildWithTagAndContent(const char *tag, const char *content);
bool HasChildWithContent(const char *content);
void RemoveAllChildren(void);
void RemoveChildByIndex(long index);
void AccumulateTagContent(const char *tag, const char *skipTags, CkString &out);
void GetChildContentByIndex(long index, CkString &content);
void GetChildTag(long index, CkString &tag);
void AddToAttribute(const char *name, long amount);
void AddToContent(long amount);
void AddToChildContent(const char *tag, long amount);
void AddOrUpdateAttributeI(const char *name, long value);
void AddOrUpdateAttribute(const char *name, const char *value);
bool HasAttribute(const char *name);

Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.