VC parses the XML file detailed use of CMarkup
- 2020-04-01 23:40:19
- OfStack
VC parses XML files with tools like CMarkup, tinyXML, IBM, MS, and so on.
TinyXML is said to be very good, maybe the character set problem, I can't compile.
So I used CMarkup to parse it. After using it, I found it very good and convenient to use.
CMarkup download address: http://www.firstobject.com/
Find a method on the Internet is very method, I took it down
I. generate userinfo.xml
Browse for specific elements
Three, modify,
Fourth, add
1 Add at the end (use AddElem)
2 Add in front (using InsertElem)
Five, delete,
Six, find
See second point for browsing. There are no properties in the example above, but there are properties in the example below
Seven,
Eight, to find the
Nine, modify,
CMarkup character set problem
CMarkup supports Unicode, _MBCS macro definition. VS is set to the MBCS macro definition by default and utf-8 encoding if the macro is not set.
MBCS macro definition, and the open file is utf-8 encoding:
CMarkup converts utf-8 to ANSI encoding when it is loaded, so it is set where it is read
# include < The locale. H >
The setlocale (LC_ALL, "");
There is no MBCS macro definition, and the open file is utf-8:
By default, the system reads files into the system in utf-8 format, so the data should be manually converted to ANSI format for display
# include < The locale. H >
The setlocale (LC_ALL, "");
StrVal = CMarkup: : UTF8ToA (strVal);
PS: can be fixed by copying functions such as strcpy.
TinyXML is said to be very good, maybe the character set problem, I can't compile.
So I used CMarkup to parse it. After using it, I found it very good and convenient to use.
CMarkup download address: http://www.firstobject.com/
Find a method on the Internet is very method, I took it down
//----------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>
I. generate userinfo.xml
CMarkup xml;
xml.SetDoc("xml version="1.0" encoding="UTF-8"?>rn");
xml.AddElem("UserInfo");
xml.IntoElem();
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"?>rn");
xml.AddElem("UserInfo");
xml.IntoElem();
xml.AddElem("UserID","luo");
xml.AddElem("UserID","lin");
xml.OutOfElem();
xml.Save("UserInfo.xml");
Browse for specific elements
//-- view all userids --
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();
while (xml.FindChildElem("UserID");)
{
//Here is the contact or the parent contact & NBSP; & have spent & have spent & have spent
CString strTagName = _T("");
CString strData = _T("");
strTagName = xml.GetChildTagName();
strData = xml.GetChildData();
TRACE("n---tagName:%s,Data:%s--n",strTagName,strData);
}
/************** That's ok **********************
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();
xml.FindElem(); //UserInfo
xml.IntoElem();
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);
}
*/
//-- -- -- -- -- -- -- -- -- -- -- -- -- the -- -- -- -- -- -- -- -- -- -- & have spent & have spent & have spent & have spent
---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");)
{
//The contact is still the parent
CString strTagName = _T("");
CString strData = _T("");
strTagName = xml.GetChildTagName();
strData = xml.GetChildData();
TRACE("n---tagName:%s,Data:%s--n",strTagName,strData);
}
/************** That's ok **********************
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();
xml.FindElem(); //UserInfo
xml.IntoElem();
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);
}
*/
//-- -- -- -- -- -- -- -- -- -- -- -- -- the -- -- -- -- -- -- -- -- -- --
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin -
Three, modify,
//-- change the UserID from "luo" to "flypigluo" --
BOOL bLoadXml = false;
CMarkup xml;
bLoadXml = xml.Load("UserInfo.xml");
if (bLoadXml)
{
CString strUserID = _T("");
xml.ResetMainPos();
xml.FindElem();
xml.IntoElem();
while (xml.FindElem("UserID"))
{
strUserID = xml.GetData();
if (strUserID=="luo")
{
xml.SetData(CString("flypig")+strUserID);
xml.Save("UserInfo.xml");
break;
}
}
}
//-- change the UserID from "luo" to "flypigluo" --
BOOL bLoadXml = false;
CMarkup xml;
bLoadXml = xml.Load("UserInfo.xml");
if (bLoadXml)
{
CString strUserID = _T("");
xml.ResetMainPos();
xml.FindElem();
xml.IntoElem();
while (xml.FindElem("UserID"))
{
strUserID = xml.GetData();
if (strUserID=="luo")
{
xml.SetData(CString("flypig")+strUserID);
xml.Save("UserInfo.xml");
break;
}
}
}
Fourth, add
1 Add at the end (use AddElem)
//Add it at the end
BOOL bLoadXml = false;
CMarkup xml;
bLoadXml = xml.Load("UserInfo.xml");
if (bLoadXml)
{
xml.ResetMainPos();
xml.FindElem();
xml.IntoElem();
xml.AddElem("UserID","luoluo");
xml.OutOfElem();
xml.Save("UserInfo.xml");
}
//-- formerly --& NBSP; & have spent & have spent & have spent
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>
//-- added as --& NBSP; -- & have spent & have spent & have spent
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
<UserID>luoluo</UserID>
</UserInfo>
2 Add in front (using InsertElem)
BOOL bLoadXml = false;
CMarkup xml;
bLoadXml = xml.Load("UserInfo.xml");
if (bLoadXml)
{
xml.ResetMainPos();
xml.FindElem();
xml.IntoElem();
xml.InsertElem("UserID","AddUserIDHead");
xml.OutOfElem();
xml.Save("UserInfo.xml");
}
//-- formerly -- & NBSP; & have spent & have spent & have spent
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>
//-- revised --& NBSP; & have spent & have spent & have spent
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.IntoElem();
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>
Five, delete,
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();
while (bFind)
{
bFind = xml.FindChildElem("UserID");
if (bFind)
{
//Here is the contact or the parent contact & NBSP; & have spent & have spent & have spent
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)
{
//The contact is still the parent
CString strData = _T("");
strData = xml.GetChildData();
if (strData=="luo")
{
xml.RemoveChildElem();
xml.Save("UserInfo.xml");
break;
}
}
}
Six, find
See second point for browsing. There are no properties in the example above, but there are properties in the example below
//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>
Seven,
CString strID = _T("");
CString strPwd = _T("");
CMarkup xml;
xml.SetDoc("xml version="1.0" encoding="UTF-8"?>");
xml.AddElem("UserInfo");
xml.IntoElem();
for (int i=0; i<10; i++)
{
//Generates a random number as permission & PI; & have spent & have spent & have spent
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.IntoElem();
for (int i=0; i<10; i++)
{
//Generates a random number as a 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");
Eight, to find the
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");
}
Nine, modify,
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 problem
CMarkup supports Unicode, _MBCS macro definition. VS is set to the MBCS macro definition by default and utf-8 encoding if the macro is not set.
MBCS macro definition, and the open file is utf-8 encoding:
CMarkup converts utf-8 to ANSI encoding when it is loaded, so it is set where it is read
# include < The locale. H >
The setlocale (LC_ALL, "");
There is no MBCS macro definition, and the open file is utf-8:
By default, the system reads files into the system in utf-8 format, so the data should be manually converted to ANSI format for display
# include < The locale. H >
The setlocale (LC_ALL, "");
StrVal = CMarkup: : UTF8ToA (strVal);
PS: can be fixed by copying functions such as strcpy.