C++读取xml有很多第三方的xml解析库,最近使用tinyxml库来解析,下面直接上应用例子:

Skin.xml文档内容如下:

<UI>
<Image name="banner" x="0" y= "0" width="900" height="40" img_src="\img\ui\banner.jpg" />
<Image name="logo" x="5" y= "5" width="32" height="32" img_src="\img\ui\logo.png" />
<Image name="logo" x="863" y= "5" width="32" height="32" img_src="\img\ui\close.png" />
<Button id="1001" x="540" y= "360" width="48" height="48" img_normal="\img\ui\btn_start_normal.png" img_hover="\img\ui\btn_start_hover.png" img_down="\img\ui\btn_start_normal.png" />
<Button id="1002" x="540" y= "480" width="48" height="48" img_normal="\img\ui\btn_stop_normal.png" img_hover="\img\ui\btn_stop_hover.png" img_down="\img\ui\btn_stop_normal.png" />
<Button id="1003" x="250" y= "5" width="48" height="48" img_normal="\img\ui\btn_pre_normal.png" img_hover="\img\ui\btn_pre_hover.png" img_down="\img\ui\btn_pre_normal.png" />
<Button id="1004" x="250" y= "845" width="48" height="48" img_normal="\img\ui\btn_next_normal.png" img_hover="\img\ui\btn_next_hover.png" img_down="\img\ui\btn_next_normal.png" />
</UI>

  C++利用tinyxml库读取的关键代码如下:(这里得先说明下,下面代码中的MyButton类是我自己自定义的button类,实现原理和代码在我的另一篇文章:http://www.cnblogs.com/JczmDeveloper/p/3494615.html):

#include "tinyxml/tinyxml.h"
void LoadSkin(LPCTSTR lpszRelativePath,LPCTSTR lpszXmlName)
{ CString strCurDir = Util::GetCurrentDir();//获取当前目录
CString strRelativePath = lpszRelativePath;
CString strXmlName = lpszXmlName;
strCurDir += strRelativePath; CString strXmlPath = strCurDir + L"\\"+ strXmlName;
USES_CONVERSION;
LPCSTR lpStr =NULL;
lpStr = T2A(strXmlPath.GetBuffer(strXmlPath.GetLength()));
//加载xml文件
TiXmlDocument* pDoc = new TiXmlDocument(lpStr);
bool bLoadOk = pDoc->LoadFile();
//读取xml文件
CString strX ,strY,strWidth,strHeight,strImgSrc;
int nX =0,nY=0,nWidth=0,nHeight=0;
int nID = 0;
CString strID,strBtnNormal,strBtnHover,strBtnDown;
CString strElementType;
CString strAttrName = L""; TiXmlElement* pRootElement = pDoc->RootElement();
TiXmlElement* pElement = pRootElement->FirstChildElement();
while(pElement)
{ strElementType = pElement->Value();
if(strElementType == L"Button")
{
//读取当前元素节点
TiXmlAttribute* pAttribute = pElement->FirstAttribute();
while(pAttribute)
{
strAttrName = pAttribute->Name();
if(strAttrName ==L"id")
strID = pAttribute->Value();
else if(strAttrName == L"x")
strX =pAttribute->Value();
else if(strAttrName == L"y")
strY = pAttribute->Value();
else if(strAttrName == L"width")
strWidth = pAttribute->Value();
else if(strAttrName == L"height")
strHeight = pAttribute->Value();
else if(strAttrName == L"img_normal")
strBtnNormal = pAttribute->Value();
else if(strAttrName == L"img_hover")
strBtnHover = pAttribute->Value();
else if(strAttrName == L"img_down")
strBtnDown = pAttribute->Value();
pAttribute = pAttribute->Next();
} nID = _ttoi(strID);
nX = _ttoi(strX);
nY = _ttoi(strY);
nWidth = _ttoi(strWidth);
nHeight = _ttoi(strHeight); CString szDir = Util::GetCurrentDir();
strBtnNormal = szDir + strBtnNormal;
strBtnHover = szDir + strBtnHover;
strBtnDown = szDir + strBtnDown;
CRect rt;
rt.top = nX;
rt.left = nY;
rt.right = rt.left + nWidth;
rt.bottom = rt.top + nHeight;
MyButton* pButton = new MyButton;
pButton->SetBtnBmp(strBtnNormal,strBtnHover,strBtnDown);
pButton->Create(m_hWnd,rt,NULL,WS_CHILD|WS_VISIBLE);
pButton->SetBtnID(nID);
}
else if(strElementType == L"Image")
{
//读取当前元素节点
TiXmlAttribute* pAttribute = pElement->FirstAttribute();
while(pAttribute)
{
strAttrName = pAttribute->Name();
if(strAttrName == L"x")
strX =pAttribute->Value();
else if(strAttrName == L"y")
strY = pAttribute->Value();
else if(strAttrName == L"width")
strWidth = pAttribute->Value();
else if(strAttrName == L"height")
strHeight = pAttribute->Value();
else if(strAttrName == L"img_src")
strImgSrc = pAttribute->Value();
pAttribute = pAttribute->Next();
}
nX = _ttoi(strX);
nY = _ttoi(strY);
nWidth = _ttoi(strWidth);
nHeight = _ttoi(strHeight); CString szDir = Util::GetCurrentDir();
CString strImgPath = szDir + strImgSrc;
Graphics graphics(GetWindowDC()); Image img(strImgPath,FALSE);
graphics.DrawImage(&img,nX,nY,nWidth,nHeight);
} //下一个元素节点
pElement = pElement->NextSiblingElement();
} }

  

C++ 读取XML文件(tinyXML库的应用)的更多相关文章

  1. C语言处理xml文件的库

    读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...

  2. Android 开发自己的网络收音机4——读取XML文件的电台数据

    国内外的电台数据很多,起码有好几百,所以把这些数据都写到代码里面是不实际的.只能写成一个数据文件,程序启动的时候再去加载.保存这些简单数据,我们肯定会优先使用XML文件,今天讲讲如何读取XML里面的数 ...

  3. C#读取XML文件的方法

    先写一个xml文件: <?xml version="1.0" encoding="utf-8" ?> <bookste> <!-- ...

  4. DOM4J读取XML文件

    最近在做DRP的项目,其中涉及到了读取配置文件,用到了DOM4J,由于是刚开始接触这种读取xml文件的技术,好奇心是难免的,于是在网上又找了一些资料,这里就结合找到的资料来谈一下读取xml文件的4中方 ...

  5. 读取xml文件,写入excel

    在上一篇 Python写xml文件已经将所有订单写入xml文件,这一篇我们把xml文件中的内容读出来,写入excel文件. 输入xml格式: <?xml version="1.0&qu ...

  6. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  7. C#中常用的几种读取XML文件的方法

    1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/

  8. 读取xml文件报错:Invalid byte 2 of 2-byte UTF-8 sequence。

    程序读取xml文件后,系统报“Invalid byte 2 of 2-byte UTF-8 sequence”错误,如何解决呢? 1.程序解析xml的时候,出现Invalid byte 2 of 2- ...

  9. C#读取XML文件的基类实现

    刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node ...

随机推荐

  1. Java 声明和访问控制(一) 数组的声明 private可以修饰类吗

    数组的声明: int []a[] = new int[4][];//是正确的 int[] array = new int[2]{1,2};//是错误的 数组的长度是不可改变的,不能通过任何方式改变大小 ...

  2. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  3. 【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)

    SORTBIT - Sorted bit squence no tags Let's consider the 32 bit representation of all integers i from ...

  4. winform异型不规则界面设计

    一,不规则WINFORM窗体 Author:unknown From:Internet在以前版本的Visual Basic或Visual C++中,创建不规则窗体和控件是一件很复杂的事,不仅需要调用大 ...

  5. SharePoint 2010 母版页制作的简单介绍

    转:http://www.cnblogs.com/jianyus/archive/2012/01/11/2319621.html 1.  首先打开SharePoint Designer 2010,找到 ...

  6. 系统交易策略 hylt

    最令我尴尬的事情,莫过于很多朋友来到网站,不知道我说的是什么.大多数人以为鬼仆是推销软件的.其实这里理解是错的,特别是一些软件制作与经销商,更出 于推销的目的,故意夸大产品性能,模糊交易系统与一般行情 ...

  7. Java基础(二十三)GUI图形界面编程(Java基础完)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  8. JavaScript---网络编程(12)--DHTML技术演示(5)-form表单验证技术(正则)

    这里不进行很复杂的后台验证以及JavaScript的正则表达式,只是简单的介绍下这个技术,简单的后台接收与跳转,大概了解怎么验证的就可以.具体的技术,我后面还会继续写博客的.本人也还在学习中. 表单验 ...

  9. vijosP1038 添加括号

    vijosP1038 添加括号 链接:https://vijos.org/p/1038 [思路] 区间DP. 本题的关键在于如何输出解.对于求和表达式而言可以用一个p[][]记录决策然后递归输出,对于 ...

  10. 2 weekend110的hadoop的自定义排序实现 + mr程序中自定义分组的实现

    我想得到按流量来排序,而且还是倒序,怎么达到实现呢? 达到下面这种效果, 默认是根据key来排, 我想根据value里的某个排, 解决思路:将value里的某个,放到key里去,然后来排 下面,开始w ...