tinyxml2解析类

tinyxml2原来 tinyxml升级版版

1、下载

https://sourceforge.net/projects/tinyxml/

https://github.com/leethomason/tinyxml2

2、最新版本 最好在vs2010以上版本运行(包含#include <stdint.h>)

3、vs2008使用tinyxml2  解决办法 下载任意头文件 放到Microsoft Visual Studio 9.0\VC\include 目录即可。

    1)、http://www.azillionmonkeys.com/qed/pstdint.h

    2)、http://msinttypes.googlecode.com/svn/trunk/stdint.h

    或者直接放到工程目录,不过要将tinyxml2.h头文件更改下 #include <stdint.h> 改为 #include "stdint.h"

4、右键工程属性更改编译属性 c/c++  -->预编译头-->不使用预编译头。

5、看示例:

test.xml文件

<?xml version="1.0"?>
<root name="hello">
    <item>
        <eye>0</eye>
        <front>10</front>
    </item>
    <item>
        <center>0 10 -10</center>
        <radius>10</radius>
    </item>
</root>
6、c++代码:

#include "stdafx.h"
#include "tinyxml2.h"
#include <iostream>
 
 
using namespace std;
using namespace tinyxml2;
 
int main(int argc, char* argv[])
{
    XMLDocument doc;
    if (doc.LoadFile("test.xml"))
    {
        doc.PrintError();
        exit(1);
    }
 
    // 根元素
    XMLElement* pRoot = doc.RootElement();
    cout << "name:" << pRoot->Attribute("name") << endl << endl;
 
    // 遍历<item>元素
    XMLElement* pItem = pRoot->FirstChildElement("item");
    while (pItem) {
        // 遍历属性列表
        const XMLAttribute* surfaceAttr = pItem->FirstAttribute();
        while (surfaceAttr)
        {
            cout << surfaceAttr->Name() << ":" << surfaceAttr->Value() << " ";
            surfaceAttr = surfaceAttr->Next();
        }
        cout << endl;
        // 遍历子元素
        XMLElement* itemChild = pItem->FirstChildElement();
        while (itemChild)
        {
            cout << itemChild->Name() << " = " << itemChild->GetText() << endl;
            itemChild = itemChild->NextSiblingElement();
        }
        cout << endl;
        pItem = pItem->NextSiblingElement("item");
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值