The VC parsing xml--uses the CMarkup class to parse the XML. --yxifu
(a) First to talk about the characters in the XML, manually fill in the note.
Character entity
& & or & #38; #38;
' ' or & #39;
> > or & #62;
< < or & #38;& #60;
"" or & #34;
(ii) The source code of the CMarkup class.
This is currently the latest version;
This is the official website sample file, take out the inside of Markup.cpp and Markup.h, import your project inside, CMarkup class can use;
Download Address: Http://www.firstobject.com/Markup90.zip October 1, 2007 the latest version of the official website
If you can't download it, you can go to the official website to download http://www.firstobject.com/dn_markup.htm
(iii) Create an XML document.
CMarkup XML;
Xml. Addelem ("ORDER");
Xml. Addchildelem ("ITEM");
Xml. Intoelem ();
Xml. Addchildelem ("SN", "132487a-j");
Xml. Addchildelem ("NAME", "Crank casing");
Xml. Addchildelem ("QTY", "1");
Xml. Save ("C://userinfo.xml");
The effect is as follows:
CMarkup XML;
(XML. Findchildelem ("ITEM"))
{ XML. Intoelem (); XML. Findchildelem ("SN"); CString cssn = XML. Getchilddata (); XML. Findchildelem ("QTY");
Nqty = Atoi (XML. Getchilddata ()); XML. Outofelem ();
}
Add in the last face, use Addelem; add at the top, use Insertelem.
CMarkup XML;
Xml. Load ("C://.xml");
Xml. Addelem ("ORDER");
Xml. Intoelem ();
XML. Addelem ("ITEM"); XML. Intoelem (); XML. Addelem ("SN", "4238764-a" ); add element XML. Addelem ("NAME", "bearing" );//add element XML. Addelem ("QTY", " " ");//Add Element
Xml. Addelem ("SHIPMENT");
Xml. Intoelem ();
Xml. Addelem ("POC");//add Element
XML. Setattrib ("type", "non-emergency");//Add attribute
XML. Intoelem ();
Xml. Addelem ("NAME", "John Smith");//add Element
XML. Addelem ("TEL", "555-1234");//add Element
XML. Save ("C://.xml");
The effect is as follows:
(vi) Modifying elements and attributes
If the attribute type in the POC is changed to: change;
Element Tel changed to: 123456789
CMarkup XML;
if (XML. Load ("Userinfo.xml"))
{
CString strUserID = _t ("");
Xml. Resetmainpos ();
if (XML. Findchildelem ("SHIPMENT"))
{
Xml. Intoelem ();
if (XML. Findchildelem ("POC"))
{
Xml. Intoelem ();
CString Str_type=xml. Getattrib ("type");
MessageBox (Str_type);
Xml. Setattrib ("type", "change");
strUserID = XML. GetData ();
if (XML. Findchildelem ("TEL"))
{
Xml. Intoelem ();
Xml. SetData ("123456789");
Xml. Save ("Userinfo.xml");
Return
}
}
}
}
(vii) Delete elements:
Delete the Sn=132487a-j item.
CMarkup XML;
if (XML. Load ("Userinfo.xml"))
{
CString strUserID = _t ("");
Xml. Resetmainpos ();
if (XML. Findchildelem ("ITEM"))
{
Xml. Intoelem ();
CString STR_SN;
Xml. Findchildelem ("SN");
Str_sn=xml. Getchilddata ();
if (str_sn== "132487a-j")
{
Xml. Removeelem ();
Xml. Save ("Userinfo.xml");
}
}
}
Transferred from: http://hi.baidu.com/yxifu/blog/item/fa1569225bda52a44623e8f0.html