How to use TinyXML to manipulate XML and pay attention to problems _c language

Source: Internet
Author: User
Tags assert garbage collection joins
In the previous blog, I probably learned about the architecture of the TinyXML tool, and this blog, we detail how to manipulate XML using TinyXML. And in the process of operation, we should pay attention to the problem.
First of all, the TinyXML source files into their own projects, the effect is as follows:

In fact, add files directly to the project: as follows:

If your system is win7 or office is more than 2007, adding files This function is not available, the result is: or directly crashes. or no response. The solution is as follows:
Http://www.jb51.net/article/32996.htm
The environment is ready, so let's start the operation. As with database table operations---Additions and deletions to check. Based on this logic, it starts with the increment (creation).
The format created is as follows:
Copy Code code as follows:

<Persons>
<Person>
<name>lhy</name>
<age>22</age>
</Person>
</Persons>

In the previous blog, we also introduced all the classes in the TinyXML parser and the relationships between classes.
To create the XML in the format above, the code is as follows:
Copy Code code as follows:

Creates a document object for XML.
Tixmldocument *mydocument = new Tixmldocument ();
Creates a root element and joins it.
Tixmlelement *rootelement = new Tixmlelement ("Persons");
Mydocument->linkendchild (rootelement);
Creates a person element and joins it.
Tixmlelement *personelement = new Tixmlelement ("person");
Rootelement->linkendchild (personelement);
Creates a name element, an age element, and joins.
Tixmlelement *nameelement = new Tixmlelement ("name");
Tixmlelement *ageelement = new Tixmlelement ("Age");
Personelement->linkendchild (nameelement);
Personelement->linkendchild (ageelement);
Sets the contents of the name element and the age element and joins it.
Tixmltext *namecontent = new Tixmltext ("Lhy");
Tixmltext *agecontent = new Tixmltext ("22");
Nameelement->linkendchild (namecontent);
Ageelement->linkendchild (agecontent);
Mydocument->savefile ("D:\\lhy\\xml.txt");//Save to File

As long as you understand the relationships between nodes in the XML, creating is not a problem. Plainly is a seniority relationship.
Created, but as a C + + program ape, after writing, always feel a little awkward, always feel what is wrong. Do you see any of the problems in it?
Yes, a little bit of code has a lot of new pointers. In C + + there is no garbage collection mechanism in Java, and you have to deal with these discarded rubbish yourself. But there is no DELETE statement in the code?
On the Internet to check the data, found that many of the creation code, there is no DELETE statement? Is everyone copying and pasting? Or is tinyxml in the funny?
I summed up the following points, but in the end the development process is still a question, but the development process, there is no problem, so my program for the time being.
Say one: Many articles, are new no delete, because TinyXML can automatically release, automatically destroy the pointer, without the developer manual release.
Question: Can the new release be released automatically? New came out to show that it was created on the heap and when will it be automatically released? Automatic release at the end of the program? So how do you judge the end of the procedure? (in one module how to deconstruct the memory area of another module, we will talk about later), so this argument is self-defeating.
Since TinyXML is a self-destruct feature, we query its source code and find that it is, in TinyXML, a corresponding pointer release in the destructor. But not every node is so.
The details of the source code:
Copy Code code as follows:

Tixmlnode::~tixmlnode ()
{
tixmlnode* node = firstchild;
tixmlnode* temp = 0;
while (node)
{
temp = node;
node = node->next;
Delete temp;
}
}
void Tixmlnode::clear ()
{
tixmlnode* node = firstchild;
tixmlnode* temp = 0;
while (node)
{
temp = node;
node = node->next;
Delete temp;
}
FirstChild = 0;
LastChild = 0;
}

We also know that there are inheritance relationships between classes in TinyXML.
So let's look at the Tixmlelement class in TinyXML:
Copy Code code as follows:

Tixmlelement::~tixmlelement ()
{
Clearthis ();
}
void Tixmlelement::clearthis ()
{
Clear ();
while (Attributeset.first ())
{
tixmlattribute* node = Attributeset.first ();
Attributeset.remove (node);
Delete node;
}
}

Because Tixmlelement is an inherited Tixmlnode, the destructor of the Tixmldocument class is not found in tixmldocument.
The second argument: Tixmldocument object is the root of the tree, in a complete document, in addition to it, the rest of the node must be its descendants, so TinyXML used a very clever way to deconstruct the corresponding object of each node---- Each node's destructor is delegated to its father, so that if the father is properly refactored, or if the father's clear function is invoked, all of its descendants will be properly refactored, so as long as the Tixmldocument object is correctly refactored for the entire document.
In the above code created, we found that all nodes are attached to the root node.
In fact this code: Mydocument->linkendchild (rootelement), the use of polymorphic methods. The relationships between classes are as follows:

and the Linkendchild source code is as follows: It is a method in the parent class Tixmlnode
Copy Code code as follows:

tixmlnode* Tixmlnode::linkendchild (tixmlnode* node)
{
ASSERT (Node->parent = = 0 | | node->parent = = this);
ASSERT (node->getdocument () = = 0 | | node->getdocument () = = This->getdocument ());
if (node->type () = = Tixmlnode::D ocument)
{
Delete node;
if (GetDocument ()) GetDocument ()->seterror (tixml_error_document_top_only, 0, 0, Tixml_encoding_unknown);
return 0;
}
Node->parent = this;
Node->prev = LastChild;
Node->next = 0;
if (LastChild)
Lastchild->next = node;
Else
FirstChild = node;//It is an empty list.
LastChild = node;
return node;
}

In this case: if you delete the root node, mydocument in the program is equivalent to deleting the Tixmlnode, which is equivalent to calling the Tixmlnode destructor.
Question: The Web says this way, the deconstruction is from leaves to roots. According to the destructor in Tixmlnode, we can draw from the roots to the leaves.
But when we delete mydocument, we should be aware of one thing:
When you create a document, that is, mydocument in the program segment. If you create from a heap, you need to manually release it. As we have in the above fragment, it is created on the heap.
Tixmldocument *mydocument=new tixmldocument ();
If created from the stack, we do not need to manually release it, but the program automatically invokes the destructor. At the same time, we should note that other elements must be created on the heap. Because in the Tixmlnode destructor, it is delete, but the Dongdong on the stack is not required to delete, so the descendant nodes that are connected except the root node must be created from the heap.
After we explain, understand the principle of tinyxml? As long as you understand the role of the class in tinyxml and the relationship between classes, see the source is no problem drop oh.
This blog explains some of the questions that exist based on the creation of XML small demos. In the next blog, we will answer questions in the parsing XML based on parsing.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.