Detailed explanation of Using VC to parse XML files-CMarkup

Source: Internet
Author: User

There are many tools for VC to parse XML files, such as CMarkup, tinyXML, IBM, and MS.
It is said that tinyXML is very good and may have character set problems. I cannot compile it.
Therefore, CMarkup is used for parsing. after use, it is very good and convenient to use.
CMarkup: http://www.firstobject.com/
I just picked it up when I found that there is a good way to do it online. Copy codeThe Code is as follows: // ---------- UserInfo. xml --------------
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>
// ---------- UserInfo. xml --------------
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>

1. Generate UserInfo. xml
Copy codeThe Code is as follows: CMarkup xml;
Xml. SetDoc ("xml version = \" 1.0 \ "encoding = \" UTF-8 \ "?> \ R \ n ");
Xml. AddElem ("UserInfo ");
Xml. cancelem ();
Xml. AddElem ("UserID", "luo ");
Xml. AddElem ("UserID", "lin ");
Xml. OutOfElem ();
Xml. Save ("UserInfo. xml ");
CMarkup xml;
Xml. SetDoc ("xml version = \" 1.0 \ "encoding = \" UTF-8 \ "?> \ R \ n ");
Xml. AddElem ("UserInfo ");
Xml. cancelem ();
Xml. AddElem ("UserID", "luo ");
Xml. AddElem ("UserID", "lin ");
Xml. OutOfElem ();
Xml. Save ("UserInfo. xml ");

2. Browse specific elements
Copy codeThe Code is as follows: // ---- view all userids ----
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();
While (xml. FindChildElem ("UserID ");)
{
// At this time, the contact is still the parent contact
CString strTagName = _ T ("");
CString strData = _ T ("");
StrTagName = xml. GetChildTagName ();
StrData = xml. GetChildData ();
TRACE ("\ n --- tagName: % s, Data: % s -- \ n", strTagName, strData );
}
**************** **
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();

While (xml. FindChildElem ("UserID ");)
{
Xml. cancelem ();
CString strTagName = _ T ("");
CString strData = _ T ("");
StrTagName = xml. GetTagName ();
StrData = xml. GetData ();
TRACE ("\ n --- tagName: % s, Data: % s -- \ n", strTagName, strData );
Xml. OutOfElem ();
}*/
**********************
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();
Xml. FindElem (); // UserInfo
Xml. cancelem ();
While (xml. FindElem ("UserID ");)
{
CString strTagName = _ T ("");
CString strData = _ T ("");
StrTagName = xml. GetTagName ();
StrData = xml. GetData ();
TRACE ("\ n --- tagName: % s, Data: % s -- \ n", strTagName, strData );
}
*/

Copy codeThe Code is as follows: // ------------- result ----------
--- TagName: UserID, Data: luo --
--- TagName: UserID, Data: lin-
// ---- View all userids ----
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();
While (xml. FindChildElem ("UserID ");)
{
// At this time, the contact is still the parent contact
CString strTagName = _ T ("");
CString strData = _ T ("");
StrTagName = xml. GetChildTagName ();
StrData = xml. GetChildData ();
TRACE ("\ n --- tagName: % s, Data: % s -- \ n", strTagName, strData );
}
**************** **
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();

While (xml. FindChildElem ("UserID ");)
{
Xml. cancelem ();
CString strTagName = _ T ("");
CString strData = _ T ("");
StrTagName = xml. GetTagName ();
StrData = xml. GetData ();
TRACE ("\ n --- tagName: % s, Data: % s -- \ n", strTagName, strData );
Xml. OutOfElem ();
}*/
**********************
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();
Xml. FindElem (); // UserInfo
Xml. cancelem ();
While (xml. FindElem ("UserID ");)
{
CString strTagName = _ T ("");
CString strData = _ T ("");
StrTagName = xml. GetTagName ();
StrData = xml. GetData ();
TRACE ("\ n --- tagName: % s, Data: % s -- \ n", strTagName, strData );
}
*/
// ------------- Result ----------
--- TagName: UserID, Data: luo --
--- TagName: UserID, Data: lin-

3. Modify
Copy codeThe Code is as follows: // -------- change UserID to "luo" to "flypigluo "-----------
BOOL bLoadXml = false;
CMarkup xml;
BLoadXml = xml. Load ("UserInfo. xml ");
If (bLoadXml)
{
CString strUserID = _ T ("");
Xml. ResetMainPos ();
Xml. FindElem ();
Xml. cancelem ();
While (xml. FindElem ("UserID "))
{
StrUserID = xml. GetData ();
If (strUserID = "luo ")
{
Xml. SetData (CString ("flypig") + strUserID );
Xml. Save ("UserInfo. xml ");
Break;
}
}
}
// -------- Change UserID to "luo" to "flypigluo "-----------
BOOL bLoadXml = false;
CMarkup xml;
BLoadXml = xml. Load ("UserInfo. xml ");
If (bLoadXml)
{
CString strUserID = _ T ("");
Xml. ResetMainPos ();
Xml. FindElem ();
Xml. cancelem ();
While (xml. FindElem ("UserID "))
{
StrUserID = xml. GetData ();
If (strUserID = "luo ")
{
Xml. SetData (CString ("flypig") + strUserID );
Xml. Save ("UserInfo. xml ");
Break;
}
}
}

4. Add
1. Add it to the end (using AddElem)Copy codeThe Code is as follows: // Add it to the end
BOOL bLoadXml = false;
CMarkup xml;
BLoadXml = xml. Load ("UserInfo. xml ");
If (bLoadXml)
{
Xml. ResetMainPos ();
Xml. FindElem ();
Xml. cancelem ();
Xml. AddElem ("UserID", "luoluo ");
Xml. OutOfElem ();
Xml. Save ("UserInfo. xml ");
}
// -------- Originally ------
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>
// --------- Add it -------------
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> luo </UserID>
<UserID> lin </UserID>
<UserID> luoluo </UserID>
</UserInfo>

2. Add at the beginning (use InsertElem)Copy codeThe Code is as follows: BOOL bLoadXml = false;
CMarkup xml;
BLoadXml = xml. Load ("UserInfo. xml ");
If (bLoadXml)
{
Xml. ResetMainPos ();
Xml. FindElem ();
Xml. cancelem ();
Xml. InsertElem ("UserID", "AddUserIDHead ");
Xml. OutOfElem ();
Xml. Save ("UserInfo. xml ");
}
// ---- Originally-
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>
// ------ After modification -----
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> AddUserIDHead </UserID>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>
BOOL bLoadXml = false;
CMarkup xml;
BLoadXml = xml. Load ("UserInfo. xml ");
If (bLoadXml)
{
Xml. ResetMainPos ();
Xml. FindElem ();
Xml. cancelem ();
Xml. InsertElem ("UserID", "AddUserIDHead ");
Xml. OutOfElem ();
Xml. Save ("UserInfo. xml ");
}
// ---- Originally-
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>
// ------ After modification -----
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<UserID> AddUserIDHead </UserID>
<UserID> luo </UserID>
<UserID> lin </UserID>
</UserInfo>

5. Delete
Copy codeThe Code is as follows: CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();
While (bFind)
{
BFind = xml. FindChildElem ("UserID ");
If (bFind)
{
// At this time, the contact is still the parent contact
CString strData = _ T ("");
StrData = xml. GetChildData ();
If (strData = "luo ")
{
Xml. RemoveChildElem ();
Xml. Save ("UserInfo. xml ");
Break;
}
}
}
CMarkup xml;
Xml. Load ("UserInfo. xml ");
BOOL bFind = true;
Xml. ResetMainPos ();
While (bFind)
{
BFind = xml. FindChildElem ("UserID ");
If (bFind)
{
// At this time, the contact is still the parent contact
CString strData = _ T ("");
StrData = xml. GetChildData ();
If (strData = "luo ")
{
Xml. RemoveChildElem ();
Xml. Save ("UserInfo. xml ");
Break;
}
}
}

6. Search
See the second point. Similar to the above example, there are no attributes. The following example has attributes.Copy codeThe Code is as follows: // UserInfo1.xml
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<User UserID = "UserID00" UserPwd = "UserPwd00" UserLevel = "158"/>
<User UserID = "UserID01" UserPwd = "UserPwd01" UserLevel = "162"/>
<User UserID = "UserID02" UserPwd = "UserPwd02" UserLevel = "165"/>
<User UserID = "UserID03" UserPwd = "UserPwd03" UserLevel = "168"/>
<User UserID = "UserID04" UserPwd = "UserPwd04" UserLevel = "171"/>
<User UserID = "UserID05" UserPwd = "UserPwd05" UserLevel = "175"/>
<User UserID = "UserID06" UserPwd = "UserPwd06" UserLevel = "178"/>
<User UserID = "UserID07" UserPwd = "UserPwd07" UserLevel = "181"/>
<User UserID = "UserID08" UserPwd = "UserPwd08" UserLevel = "184"/>
<User UserID = "UserID09" UserPwd = "UserPwd09" UserLevel = "188"/>
</UserInfo>
// UserInfo1.xml
Xml version = "1.0" encoding = "UTF-8"?>
<UserInfo>
<User UserID = "UserID00" UserPwd = "UserPwd00" UserLevel = "158"/>
<User UserID = "UserID01" UserPwd = "UserPwd01" UserLevel = "162"/>
<User UserID = "UserID02" UserPwd = "UserPwd02" UserLevel = "165"/>
<User UserID = "UserID03" UserPwd = "UserPwd03" UserLevel = "168"/>
<User UserID = "UserID04" UserPwd = "UserPwd04" UserLevel = "171"/>
<User UserID = "UserID05" UserPwd = "UserPwd05" UserLevel = "175"/>
<User UserID = "UserID06" UserPwd = "UserPwd06" UserLevel = "178"/>
<User UserID = "UserID07" UserPwd = "UserPwd07" UserLevel = "181"/>
<User UserID = "UserID08" UserPwd = "UserPwd08" UserLevel = "184"/>
<User UserID = "UserID09" UserPwd = "UserPwd09" UserLevel = "188"/>
</UserInfo>

VII. Generation
Copy codeThe Code is as follows: CString strID = _ T ("");
CString strPwd = _ T ("");
CMarkup xml;
Xml. SetDoc ("xml version = \" 1.0 \ "encoding = \" UTF-8 \ "?> ");
Xml. AddElem ("UserInfo ");
Xml. cancelem ();
For (int I = 0; I <10; I ++)
{
// Generate a random number as the permission
Srand (UINT (time (NULL) + I ));
Int nLevel = rand () %256;
StrID. Format ("UserID % 02d", I );
StrPwd. Format ("UserPwd % 02d", I );
Xml. AddElem ("User ");
Xml. AddAttrib ("UserID", strID );
Xml. AddAttrib ("UserPwd", strPwd );
Xml. AddAttrib ("UserLevel", nLevel );
}
Xml. OutOfElem ();
Xml. Save ("UserInfo1.xml ");
CString strID = _ T ("");
CString strPwd = _ T ("");
CMarkup xml;
Xml. SetDoc ("xml version = \" 1.0 \ "encoding = \" UTF-8 \ "?> ");
Xml. AddElem ("UserInfo ");
Xml. cancelem ();
For (int I = 0; I <10; I ++)
{
// Generate a random number as the permission
Srand (UINT (time (NULL) + I ));
Int nLevel = rand () %256;
StrID. Format ("UserID % 02d", I );
StrPwd. Format ("UserPwd % 02d", I );
Xml. AddElem ("User ");
Xml. AddAttrib ("UserID", strID );
Xml. AddAttrib ("UserPwd", strPwd );
Xml. AddAttrib ("UserLevel", nLevel );
}
Xml. OutOfElem ();
Xml. Save ("UserInfo1.xml ");

8. Search
Copy codeThe Code is as follows: CMarkup xml;
BOOL bLoad = false;
BOOL bFind = false;
BLoad = xml. Load ("UserInfo1.xml ");
If (bLoad)
{
CString strID;
CString strPwd;
Xml. ResetMainPos ();
Xml. FindElem (); // UserInfo
While (xml. FindChildElem ("User "))
{
StrID = xml. GetChildAttrib ("UserID ");
StrPwd = xml. GetChildAttrib ("UserPwd ");
TRACE ("\ n ---- id: % s, pwd: % s ------- \ n", strID, strPwd );
If (strID = "UserID00" & strPwd = "UserPwd00 ")
{
BFind = true;
Break;
}
}
}
If (bFind)
{
TRACE ("\ n ---- find ------ \ n ");
}
Else
{
TRACE ("\ n ---- no find ------ \ n ");
}
CMarkup xml;
BOOL bLoad = false;
BOOL bFind = false;
BLoad = xml. Load ("UserInfo1.xml ");
If (bLoad)
{
CString strID;
CString strPwd;
Xml. ResetMainPos ();
Xml. FindElem (); // UserInfo
While (xml. FindChildElem ("User "))
{
StrID = xml. GetChildAttrib ("UserID ");
StrPwd = xml. GetChildAttrib ("UserPwd ");
TRACE ("\ n ---- id: % s, pwd: % s ------- \ n", strID, strPwd );
If (strID = "UserID00" & strPwd = "UserPwd00 ")
{
BFind = true;
Break;
}
}
}
If (bFind)
{
TRACE ("\ n ---- find ------ \ n ");
}
Else
{
TRACE ("\ n ---- no find ------ \ n ");
}

9. Modify
Copy codeThe Code is as follows: CMarkup xml;
BOOL bLoad = false;
BOOL bFind = false;
BLoad = xml. Load ("UserInfo1.xml ");
If (bLoad)
{
CString strID;
CString strPwd;
Xml. ResetMainPos ();
Xml. FindElem (); // UserInfo
While (xml. FindChildElem ("User "))
{
StrID = xml. GetChildAttrib ("UserID ");
StrPwd = xml. GetChildAttrib ("UserPwd ");
TRACE ("\ n ---- id: % s, pwd: % s ------- \ n", strID, strPwd );
If (strID = "UserID00" & strPwd = "UserPwd00 ")
{
BFind = true;
Xml. SetChildAttrib ("UserID", strID + CString ("Modify "));
Xml. Save ("UserInfo1.xml ");
Break;
}
}
}
If (bFind)
{
TRACE ("\ n ---- find ------ \ n ");
}
Else
{
TRACE ("\ n ---- no find ------ \ n ");
}

CMarkup Character Set Problems
CMarkup supports Unicode, _ MBCS macro definition. VS is set to MBCS macro definition by default, and UTF-8 encoding is not set for this macro.
There is a MBCS macro definition and the opened file is a UTF-8 code:
CMarkup converts the UTF-8 to the ANSI encoding format during Load, so you need to set
# Include <locale. h>
Setlocale (LC_ALL ,"");
There is no MBCS macro definition and the opened file is UTF-8 encoding:
Files are read to the system in UTF-8 format by default, so you need to manually convert the data to ANSI format for display when displaying
# Include <locale. h>
Setlocale (LC_ALL ,"");
StrVal = CMarkup: UTF8ToA (strVal );
PS: it can be corrected by copying functions such as strcpy.

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.