[TinyXML]使用范例

#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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值