CMakeUP操作XML

1.创建XMLV0

std::string CreateXMLV1()
{
	std::string strXML;
	CMarkup temXML;
	temXML.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
	temXML.AddElem("result");

	temXML.AddChildElem("version","1.0.0.1");
	temXML.AddChildElem("Response");
	temXML.SetChildAttrib("desc","sp1d3r0x");
<span style="white-space:pre">	</span>//temXML.Save("D://test");
	temXML.OutOfElem();
	strXML = temXML.GetDoc();
	return strXML;
}
这里返回的XML格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<result>
    <version>1.0.0.1</version>
    <Response desc="sp1d3r0x"/>
</result>

2.创建XMLV1

std::string CreateXMLV2()
{
	std::string strXML;
	CMarkup temXML;
	temXML.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
	temXML.AddElem("result");
	temXML.IntoElem();
	temXML.AddElem("version","1.0.0.1");
	temXML.AddElem("name","sp1d3r0X");
	temXML.OutOfElem();
	temXML.OutOfElem();
	strXML = temXML.GetDoc();
	return strXML;
}

返回的格式:
<?xml version="1.0" encoding="UTF-8"?>
<result>
    <version>1.0.0.1</version>
    <name>sp1d3r0X</name>
</result>
3.创建XMLV2
std::string CreateXMLV3()
{
	std::string strXML;
	CMarkup temXML;
	temXML.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
	temXML.AddElem("result");

	temXML.AddChildElem("itemcount","4");
	temXML.AddChildElem("version","1.0.0.1");

	temXML.AddChildElem("item");
	temXML.IntoElem();
	temXML.AddChildElem("VMSEvents");
	temXML.SetChildAttrib("timestamp","2015-12-25 17:26:30");

	
	temXML.SetChildAttrib("type","12");
	temXML.SetChildAttrib("subType","13");
	temXML.SetChildAttrib("desc","事件描述");
	temXML.OutOfElem();
	strXML = temXML.GetDoc();

	return strXML;
}
返回的格式:
<?xml version="1.0" encoding="UTF-8"?>
<result>
    <itemcount>4</itemcount>
    <version>1.0.0.1</version>
    <item>
        <VMSEvents timestamp="2015-12-25 17:26:30" type="12" subType="13" desc="事件描述"/>
    </item>
</result>

4.解析XML,这部分就比较简单了,可直接看官方文档
bool ParseXML(std::string strBody)
{
	bool bRet = true;
	CMarkup xml;
	xml.SetDoc(strBody);
	std::string strItemName("");
	std::string strUserName("");
	std::string strUserPwd("");
	while (xml.FindChildElem("item"))
	{
		strItemName = xml.GetChildAttrib("name");
		if (strItemName == "username")
			strUserName = xml.GetChildAttrib("value");
		else if (strItemName == "password")
			strUserPwd = xml.GetChildAttrib("value");
	}
	return bRet;
}

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...