My C++ Blog

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  14 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
首先到http://www.firstobject.com/下载CMarkup教学版,解压后里面是一个DEMO,将Markup.h .cpp拷贝并添加到工程中,第一次编译可能会出现预编译错误,解决的方法在Markup.cpp最前面include "stdafx.h",或者关闭预编译。
  以下转自http://www.sqlite.com.cn/MySqlite/12/211.Html
//----------UserInfo.xml--------------
<?xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>


1。生成UserInfo.xml
CMarkup xml;

xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
xml.AddElem("UserInfo");
xml.IntoElem();
xml.AddElem("UserID","luo");
xml.AddElem("UserID","lin");
xml.OutOfElem();
xml.Save("UserInfo.xml");

2。浏览特定元素
//----查看所有UserID----
CMarkup xml;
xml.Load("UserInfo.xml");

BOOL bFind = true;
xml.ResetMainPos();
while (xml.FindChildElem("UserID");)
{

//此时接点还是父接点
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.IntoElem();
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.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);

}
*/

//-------------结果----------
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin—

3。修改
//--------把UserID为”luo”改为”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;

}

}

}

4。添加

4。1 添在最后面(使用的是AddElem)
//加在最后面
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");

}

//--------原来为------

<?xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>

//---------添加后为-------------
<?xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
<UserID>luoluo</UserID>
</UserInfo>

4。2 加在最前面(使用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");

}

//----原来为—
<?xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>

 

//------修改后-----
<?xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>AddUserIDHead</UserID>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>

5。删除
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();

while (bFind)

{

bFind = xml.FindChildElem("UserID");

if (bFind)
{//此时接点还是父接点

CString strData = _T("");
strData = xml.GetChildData();

if (strData=="luo")

{

xml.RemoveChildElem();
xml.Save("UserInfo.xml");
break;

}

}

}

6。查找

见第二点浏览。类似

上面的例子里面没有属性这些东西,下面的一个例子是有属性的
//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>

7。产生
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++)
{     //产生一个随机数,作为权限

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。查找
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。修改
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");
}

posted on 2008-11-08 02:01 IP 阅读(614) 评论(0)  编辑 收藏 引用 所属分类: XML初识

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理