#include "tinyxml.h"
#pragma comment ( lib , "tinyxml.lib" )
int test(int argc, _TCHAR* argv[])
{
TiXmlDocument doc;
TiXmlDeclaration* dec = NULL;
TiXmlElement* pElement = NULL;
TiXmlAttribute* pAttr = NULL;
TiXmlNode* pNode = NULL;
TiXmlNode* pRoot = NULL;
TiXmlNode* pHead = NULL;
TiXmlText* pText = NULL;
// 添加声明
dec = NULL;
dec = new TiXmlDeclaration( "1.0" , "GB18030" , "yes" );
if( dec != NULL )
{
pNode = NULL;
pNode = doc.LinkEndChild( dec );
if( pNode == NULL )
{
delete dec;
dec = NULL;
}
}
// 添加主结点
pElement = NULL;
pElement = new TiXmlElement( "LCMS" );
if( pElement != NULL )
{
pRoot = NULL;
pRoot = doc.LinkEndChild( pElement );
if( pRoot == NULL )
{
delete pElement;
pElement = NULL;
}
}
// 添加属性
pRoot->ToElement()->SetAttribute( "VERSION" , "1.2.2.0" );
pRoot->ToElement()->SetAttribute( "COMPANY" , "INFOGO" );
// 修改字段名
pRoot->ToElement()->SetValue( "ICS" );
// 添加内容
pText = NULL;
pText = new TiXmlText( "桌面管理、内网安全" );
if( pText != NULL )
{
pRoot->InsertEndChild( *pText );
delete pText;
pText = NULL;
}
// 添加子节点
pElement = NULL;
pElement = new TiXmlElement( "HEAD" );
if( pElement != NULL )
{
pHead = NULL;
pHead = pRoot->InsertEndChild( *pElement );
delete pElement;
pElement = NULL;
}
// 添加字段
pElement = NULL;
pElement = new TiXmlElement( "AgentId" );
if( pElement != NULL )
{
pText = NULL;
pText = new TiXmlText( "lcms_agent_0001" );
if( pText != NULL )
{
pElement->InsertEndChild( *pText );
delete pText;
pText = NULL;
}
pHead->InsertEndChild( *pElement );
delete pElement;
pElement = NULL;
}
return 0;
}
int main( int argc , _TCHAR* argv[] )
{
while( true )
{
test( argc , argv );
Sleep( 1 );
}
getchar();
}
本文通过一个具体的示例演示了如何使用TinyXML库创建XML文件的过程。包括添加XML声明、创建根元素、设置属性、插入文本节点及子元素等基本操作。
581

被折叠的 条评论
为什么被折叠?



