Tinyxml usage notes and summary

Source: Internet
Author: User

In tinyxml, some classes are defined based on various elements of XML:

Tixmlbase: The base class of the entire tinyxml model.

Tixmlattribute: attribute corresponding to the element in XML.

Tixmlnode: corresponds to a node in the DOM structure.

Tixmlcomment: corresponds to the comment in XML.

Tixmldeclaration: corresponds to the declarative part in XML, that is, <? Versiong = "1.0"?>.

Tixmldocument: corresponds to the entire XML document.

Tixmlelement: Elements corresponding to XML.

Tixmltext: corresponds to the text section of XML.

Tixmlunknown: corresponds to the unknown part of XML.

Tixmlhandler: defines some XML operations.

For example:


Go to the toy store!
Do bills

Entire Object Tree:

Tixmldocument "demo. xml"

Tixmldeclaration "version = '1. 0 ′"

"Standalone = No"
Tixmlcomment "Our to do list data"

Tixmlelement "Todo"

Tixmlelement "item"

Attribtutes: Priority = 1

Tixmltext "go to"
Tixmlelement "bold"
Tixmltext "toy store! "
Tixmlelement "item" attributes: Priority = 2
Tixmltext "Do bills"
In tinyxml, when you use firstchild ("name") to find a node, the node that calls the firstchild function must have a parent-child relationship with the node to be searched ".
Handle
It is important to check whether the return value after a method call is null if you want to read an XML document with robustness. A secure error detection implementation may produce code like this:

Of course, you can also use the multibytetowidechar and widechartomultibyte functions to implement the conversion by yourself. The above are some examples of simple applications. I believe you can write the code that meets your needs.
Tixmlelement * root = Document. firstchildelement ("document ");
If (Root)
{
Tixmlelement * element = root-> firstchildelement ("element ");
If (element)
{
Tixmlelement * child = element-> firstchildelement ("child ");
If (child)
{
Tixmlelement * child2 = Child-> nextsiblingelement ("child ");
If (child2)
{
// Finally do something useful.

Using a handle will not be so lengthy. Using the tixmlhandle class, the previous Code will become like this:
Tixmlhandle dochandle (& document );
Tixmlelement * child2 = dochandle. firstchild ("document"). firstchild ("element"). Child ("child", 1). toelement ();
If (child2)
{
// Do something useful

1. Read XML and set the node text
The following XML snippet:

8

69

1
1
/Mnt/share/1.bmp
0

/Mnt/share/2.bmp
2
1
0

.

To set the value of broadcast_version node 8 to another value, refer to the following code (add the value to 1 ):
Replace with the replaceChild (tixmlnode * replacethis, const tixmlnode & withthis) Method
Tixmldocument DOC ("Zapp. conf ");
Doc. LoadFile ();
Tixmlhandle dochandle (& Doc );
Tixmlelement * broadcast_ver = dochandle. firstchild ("zxml"). firstchild ("Zapp"). firstchild ("vbs_runtime_params"). firstchildelement ("broadcast_version"). toelement ();
Tixmlnode * oldnode = broadcast_ver-> firstchild ();
Const char * ver = broadcast_ver-> gettext ();
Int oldver = atoi (VER );
Cstring newver;
Newver. Format ("% d", oldver + 1 );
Tixmltext newtext (newver );
Broadcast_ver-> replaceChild (oldnode, newtext );
Afxmessagebox (broadcast_ver-> gettext (); // output value
Doc. SaveFile ();
Ii. Delete nodes and attribute values
The removechild (tixmlnode * removethis) method deletes the child node of the parent node,
Removeattribute (const char * Name) method to delete attribute values.
For example, deleting a broadcast_version Node
Tixmlhandle dochandle (& Doc );
Tixmlelement * broadcast_ver = dochandle. firstchild ("zxml"). firstchild ("Zapp"). firstchild ("vbs_runtime_params"). toelement ();
Tixmlnode * node = broadcast_ver-> firstchild ("broadcast_version ");
Broadcast_ver-> removechild (node );
You can also delete the entire source_1 node:
Tixmlhandle dochandle (& Doc );
Tixmlelement * broadcast = dochandle. firstchild ("zxml"). firstchild ("Zapp"). firstchild ("vbs_runtime_params"). firstchild ("broadcast"). toelement ();
Tixmlnode * node = broadcast-> firstchild ("source_1 ");
Broadcast-> removechild (node );
Delete the info attribute of broadcast_version:
Tixmlhandle dochandle (& Doc );
Tixmlelement * broadcast_ver = dochandle. firstchild ("zxml"). firstchild ("Zapp"). firstchild ("vbs_runtime_params"). firstchildelement ("broadcast_version"). toelement ();
Broadcast_ver-> removeattribute ("info"); // Delete info
You can use the nextsiblingelement () method to implement recursive deletion.
3. Add nodes and attribute values
For example, add the broadcast_pid node under source_3:
Tixmlhandle dochandle (& Doc );
Tixmlelement * broadcast = dochandle. firstchild ("zxml"). firstchild ("Zapp"). firstchild ("vbs_runtime_params"). firstchild ("broadcast"). toelement ();
Tixmlelement * broadcast_pid = new tixmlelement ("broadcast_pid ");
Tixmltext * text = new tixmltext ("7215 ");
Broadcast_pid-> setattribute ("info", "the PID ");
Broadcast_pid-> linkendchild (text );
Broadcast-> linkendchild (broadcast_pid );
A new node will be added after source_3:
7215
4. Finally, let's talk about Chinese garbled characters.
Garbled characters are caused by improper conversion between gb2312 and utf8. tinyxml is no problem in utf8 processing. When you open a utf8 document, you can specify the utf8 method during loading, or the encoding format specified in the document declaration. tinyxml is loaded according to the corresponding encoding format, but many times when we output or write Chinese fields, garbled characters may occur, regardless of the memory, or the printed content. this is because our software is usually gb2312 encoded, And the read or write content is utf8, which naturally leads to errors. two functions on the Internet can be used for conversion (the original author is unknown ):
Void convertutf8togbk (cstring & strutf8)
{
Int Len = multibytetowidechar (cp_utf8, 0, (lpctstr) strutf8,-1, null, 0 );
Unsigned short * wszgbk = new unsigned short [Len + 1];
Memset (wszgbk, 0, Len * 2 + 2 );
Multibytetowidechar (cp_utf8, 0, (lpctstr) strutf8,-1, wszgbk, Len );
Len = widechartomultibyte (cp_acp, 0, wszgbk,-1, null, 0, null, null );
Char * szgbk = new char [Len + 1];
Memset (szgbk, 0, Len + 1 );
Widechartomultibyte (cp_acp, 0, wszgbk,-1, szgbk, Len, null, null );
Strutf8 = szgbk;
Delete [] szgbk;
Delete [] wszgbk;
}

Void convertgbktoutf8 (cstring & strgbk)
{
Int Len = multibytetowidechar (cp_acp, 0, (lpctstr) strgbk,-1, null, 0 );
Unsigned short * wszutf8 = new unsigned short [Len + 1];
Memset (wszutf8, 0, Len * 2 + 2 );
Multibytetowidechar (cp_acp, 0, (lpctstr) strgbk,-1, wszutf8, Len );
Len = widechartomultibyte (cp_utf8, 0, wszutf8,-1, null, 0, null, null );
Char * szutf8 = new char [Len + 1];
Memset (szutf8, 0, Len + 1 );
Widechartomultibyte (cp_utf8, 0, wszutf8,-1, szutf8, Len, null, null );
Strgbk = szutf8;
Delete [] szutf8;
Delete [] wszutf8;
}

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.