近期在研究使用Cmarkup来操作XML文件,但网上一直都搜不到具体实用的过程与方法。经过一段时间的摸索之后,总结出一下快速入门的
几条,希望能给有需要的人以帮助。
1.首先下载Cmarkup.h和Cmarkup.cpp文件。下载网址如下,其中包括Cmarkup.h和Cmarkup.cpp及一个完整的Test工程,
http://www.firstobject.com/dn_markup.htm
2.将Cmarkup.h和Cmarkup.cpp复制到你的工程下,然后在你的对话框.cpp文件中加入#inclue"Markup.h"。现在就可以开始使用Cmarkup创建或读取XML文件了。
CMarkup xml; xml.AddElem( "ORDER" ); xml.IntoElem(); xml.AddElem( "ITEM" ); xml.IntoElem(); xml.AddElem( "SN","132487A-J" ); xml.AddElem( "NAME","crank casing" ); xml.AddElem( "QTY","1" );
其中<ORDER>是根,如下。xml.Save( "C:\\Sample.xml" );//建立XML,指定保存路径
<ORDER>
<ITEM>
<SN>132487A-J</SN>
<NAME>crank casing</NAME>
<QTY>1</QTY>
</ITEM>
</ORDER>
使用
IntoElem进入当前节点的孩子节点。然后开始创建子节点。
你可以使用GetDoc函数得到XML文件的CString类型。
MCD_STR strXML = xml.GetDoc();//MCD_STR是 Markup.h文件定义使用的字符串类型,你也可以使用自己的字符串类型,如:orstd::stringCString
4.打开XML文件
CMarkup xml;
xml.Load( "C:\\Sample.xml" );In the following example,we go inside the root ORDER element and loop through all ITEM elements with FindElem to get the serial number and quantity of each with GetData . The serial number is treated as a string and the quantity is converted to an integer using
atoi
(
MCD_2PCSZ
is defined in Markup.h to return the string's
const
pointer).
xml.FindElem(); // root ORDER element xml.IntoElem(); // inside ORDER while ( xml.FindElem("ITEM") ) { xml.IntoElem(); xml.FindElem( "SN" ); MCD_STR strSN = xml.GetData(); xml.FindElem( "QTY" ); int nQty = atoi( MCD_2PCSZ(xml.GetData()) ); xml.OutOfElem(); }找到每一个元素时,我们使用IntoElem进入她的孩子节点,使用OutOfElem返回这个节点。注意语法,每当有一个IntoElem时,就应该有一个OutOfElem与其对应。
5.添加元素和属性,添加属性时使用SetAttrib函数,如下。
CMarkup xml;
xml.AddElem( "ORDER" );
xml.IntoElem(); // inside ORDER
for ( int nItem=0; nItem<aItems.GetSize(); ++nItem )
{
xml.AddElem( "ITEM" );
xml.IntoElem(); // inside ITEM
xml.AddElem( "SN",aItems[nItem].strSN );
xml.AddElem( "NAME",aItems[nItem].strName );
xml.AddElem( "QTY",aItems[nItem].nQty );
xml.OutOfElem(); // back out to ITEM level
}
xml.AddElem( "SHIPMENT" );
xml.IntoElem(); // inside SHIPMENT
xml.AddElem( "POC" );
xml.SetAttrib( "type",strPOCType );
xml.IntoElem(); // inside POC
xml.AddElem( "NAME",strPOCName );
xml.AddElem( "TEL",strPOCTel ); 如上代码执行完后如下:
<ORDER>
<ITEM>
<SN>132487A-J</SN>
<NAME>crank casing</NAME>
<QTY>1</QTY>
</ITEM>
<ITEM>
<SN>4238764-A</SN>
<NAME>bearing</NAME>
<QTY>15</QTY>
</ITEM>
<SHIPMENT>
<POC type="non-emergency">
<NAME>John Smith</NAME>
<TEL>555-1234</TEL>
</POC>
</SHIPMENT>
</ORDER>
6.查找相应的元素
FindElem指向下一个节点。查找过程中,你可以使用ResetMainPos来重定位查找指针。Looking at the ITEM element in the above example,if someone else is creating the XML and you cannot assume the SN element is before the QTY element,then callResetMainPos
before finding the QTY element.
{
xml.IntoElem();
xml.FindElem( "SN" );
MCD_STR strSN = xml.GetData();
xml.ResetMainPos();
xml.FindElem( "QTY" );
int nQty = atoi( MCD_2PCSZ(xml.GetData()) );
xml.OutOfElem();
}
当查找的元素有一系列时,你可以使用循环来控制查找的次数。
compare the SN element data to the serial number you are searching for. By specifying the "ITEM" element tag name in the
FindElem
method we ignore all other sibling elements such as the SHIPMENT element. Also,instead of going into and out of the ITEM element to look for the SN child element,we use the
FindChildElem
and
GetChildData
methods for convenience.
xml.ResetPos(); // top of document xml.FindElem(); // ORDER element is root xml.IntoElem(); // inside ORDER while ( xml.FindElem("ITEM") ) { xml.FindChildElem( "SN" ); if ( xml.GetChildData() == strFindSN ) break; // found }
7.剩下的就是Cmarkup的可使用的函数们,由于在网页上能搜到,这里就不提供详细的解说了。原文链接:https://www.f2er.com/xml/298154.html