Tinyxml的简单使用

源码下载地址:https://sourceforge.net/projects/tinyxml/

学习可参考源码xmltest.cpp

//简单示例代码
int GerXmlConfigData()
{
    int count = 0;
    TiXmlNode *node = 0;

    TiXmlElement *todoElement = 0;
    TiXmlElement *itemElement = 0;
    vector<st_wifi_data> vec_tmp;
    
    const char *wifiname = NULL;
    const char *wifipwd  = NULL;
    const char *wifitype = NULL;

    const char *wifi_info =
        "<?xml version=\"1.0\"  standalone='no' >\n"
        "<Wifi_Info>\n"
        "<Item wifi_name=\"Chayan\" wifi_pwd=\"i1234\" wifi_type=\"1\" />"
        "<Item wifi_name=\"Pro\" wifi_pwd=\"99206\" wifi_type=\"1\"/>"
        "</Wifi_Info>";

    TiXmlDocument doc(FILE_PATH);
    bool loadOkay = doc.LoadFile();

    if (!loadOkay)
    {
        std::cout << "Could not load file 'wifi_info.xml'. Error=" << doc.ErrorDesc() << std::endl;
        doc.Parse(wifi_info);
        if (doc.Error())
        {
            std::cout << "Error in " << doc.Value() << ":" << doc.ErrorDesc() << std::endl;
            return -1;
        }
        loadOkay = doc.SaveFile();
        if (!loadOkay)
        {
            std::cout << "SaveFile 'wifi_info.xml' fail" << std::endl;
            return -1;
        }
        loadOkay = doc.LoadFile();
        if (!loadOkay)
        {
            std::cout << "Could not load file 'wifi_info.xml'. Error=" << doc.ErrorDesc() << std::endl;
            return -1;
        }
    }

    std::cout << "** Printing via operator<< **\n"
              << doc << std::endl;

    node = doc.FirstChild("Wifi_Info");
    assert(node);
    todoElement = node->ToElement();
    assert(todoElement);

    for (itemElement = todoElement->FirstChildElement();
         itemElement;
         itemElement = itemElement->NextSiblingElement())
    {
        st_wifi_data st_wifidata;
        count++;
        wifiname = itemElement->Attribute("wifi_name");
        wifipwd = itemElement->Attribute("wifi_pwd");
        wifitype = itemElement->Attribute("wifi_type");

        if (wifiname == NULL || wifipwd == NULL || wifitype == NULL)
        {
            std::cout << "xml not have the attribute,please check xml file... " << std::endl;
            return -1;
        }

        st_wifidata.wifiname=wifiname;
        st_wifidata.wifipwd=wifipwd;
        st_wifidata.wifitype=wifitype;

        std::cout << count << "wifi_name=" << st_wifidata.wifiname
                  << ",wifi_pwd=" << st_wifidata.wifipwd
                  << ",wifi_type=" << st_wifidata.wifitype << std::endl;

        vec_wifi_data.push_back(st_wifidata);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值