CMakeUP操作XML

前端之家收集整理的这篇文章主要介绍了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;
}

猜你在找的XML相关文章