Windows Mobile和Wince下使用TinyXML进行Native C++的开发
背景
继续讲述Mobile Radio项目的开发,上回讲到如何把自于 www.1radio.com.au 网站的电台数据从JSON转换成XML。这回讲述使用tinyXML在windows mobile下进行XML的开发。
Mobile Radio项目可以参考:
Windows Mobile和Wince下的WTL(Windows Template Library)开发
Windows Mobile 和 Wince 下的 WTL(Windows Template Library) 界面开发
Windows Mobile和Wince下使用WTL进行Windows Media Player开发
简介
http://sourceforge.net/projects/tinyxml/
http://www.grinninglizard.com/tinyxml/index.html
http://www.grinninglizard.com/tinyxmldocs/index.html
环境搭建
下载
请到http://sourceforge.net/projects/tinyxml/ 下载最新的release。
新建项目
新建Smart Device项目tinyXML。
点击Next
选择平台,这里选择Windows Mobile 6 Professional SDK。
选择生成静态库,不需要MFC支持和不需要生成预编译文件,点击完成。
把下载的TinyXML源代码文件(包括CPP和H文件)拷贝到项目目录下。
把源代码文件(包括CPP和H文件)添加到项目中。
设置依赖性
在Project –> Project Dependencies设置项目依赖性,Mobile Radio依赖于TinyXML。
如果使用在Windows Mobile环境下,需要更改下面的代码。
// Microsoft compiler securityFILE* TiXmlFOpen( const char* filename, const char* mode ){//#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) // FILE* fp = 0; // errno_t err = fopen_s( &fp, filename, mode ); // if ( !err && fp ) // return fp; // return 0; //#else // return fopen( filename, mode ); //#endifreturn fopen( filename, mode );}
tinyxml.cpp文件
//#define TIXML_SAFE#define TIXML_SSCANF sscanf
tinyxml.h文件
环境搭建完毕。
使用
使用TinyXML是一个愉悦的过程,所有使用的例子都可以在源代码的xmltest.cpp文件里面找到。所以强烈建议学习和使用TinyXML前先认真阅读xmltest.cpp的代码。
简单讲一下XML文件的结构,XML的结构就是层次性(Hierarchy)文件,包含Element(节点)和Attribute(属性),下面我保留英文,因为TinyXML的接口就也是使用同样的术语。Element就是节点,可以包含Attribute和子Element(Child Elements),Attribute就是Element的属性。
下面是Mobile Radio使用TinyXML的代码。
#include "include/tinyXML/tinyXML.h"
使用TinyXML只需要引用一个头文件就可以了。
const char* CONFIGPATH = "Config\\Stations.xml";
void CMobileRadioView::LoadConfig(){ std::string p = GetCurrentPath() + std::string(CONFIGPATH); TiXmlDocument document = TiXmlDocument(p.c_str());if(!document.LoadFile()) { MessageBox(L"Can not open the config file.");return; } TiXmlHandle docHandle(&document); TiXmlElement* cityElement = docHandle.FirstChild("stations").FirstChild("city").Element(); Station* station; std::string city;while (cityElement) { city = cityElement->Attribute("name"); TiXmlElement* stationElement = cityElement->FirstChildElement("station");while (stationElement) { station = new Station(); station->City = city; station->Id = atoi(stationElement->Attribute("sid")); station->Name = stationElement->Attribute("name"); station->Image = stationElement->Attribute("image"); station->Stream = stationElement->Attribute("stream"); station->Website = stationElement->Attribute("website"); stationMap[station->Id] = station; cityStationMap.insert(CityStationMap::value_type(station->City, station)); stationElement = stationElement->NextSiblingElement(); } cityElement = cityElement->NextSiblingElement(); }}
这是读取XML配置的代码,XML配置文件的结构可以参考 转换Json到XML的JavaScript实现 。大体的文件结构是分两层,第一层是城市,第二层是具体的电台信息。
TiXmlDocument document = TiXmlDocument(p.c_str());if(!document.LoadFile()){ MessageBox(L"Can not open the config file.");return;}
把XML配置文件加载到TiXmlDocument里。
TiXmlHandle docHandle(&document);
根据TiXmlDocument生成TiXmlHandle。
TiXmlElement* cityElement = docHandle.FirstChild("stations").FirstChild("city").Element();
TinyXML不直接支持XPath,所以只能一层层读,从根节点逐层查找。如果需要XPath支持,可以参考TinyXPath (http://tinyxpath.sourceforge.net)。
while (cityElement){ city = cityElement->Attribute("name"); TiXmlElement* stationElement = cityElement->FirstChildElement("station");while (stationElement) { station = new Station(); station->City = city; station->Id = atoi(stationElement->Attribute("sid")); station->Name = stationElement->Attribute("name"); station->Image = stationElement->Attribute("image"); station->Stream = stationElement->Attribute("stream"); station->Website = stationElement->Attribute("website"); stationMap[station->Id] = station; cityStationMap.insert(CityStationMap::value_type(station->City, station)); stationElement = stationElement->NextSiblingElement(); } cityElement = cityElement->NextSiblingElement();}
循环取出城市(City)和电台(Station)信息,FirstChildElement()查找第一个子Element。NextSiblingElement()用于读取同一层的兄弟Element,Attribute可以取出Element的Attribute。关于更多的读取例子,请看xmltest.cpp的代码。
上述例子把XML配置信息读取到C++的map和multimap里面。这两个容器的定义如下:
//Id -> Stationtypedef std::map<int, Station*> StationMap; //City -> Stationtypedef std::multimap<std::string, Station*> CityStationMap;
StationMap保存ID和电台信息,一对一。CityStationMap保存城市和电台信息,一对多。
界面处理
两个容器初始化完毕以后,界面可以根据容器的信息生成。
for(CityStationMap::iterator it=cityStationMap.begin(); it!=cityStationMap.end(); ++it){if(city.compare(it->first) != 0) { city = it->first; CString c = city.c_str(); m_wndCity.AddString(c); }}
根据配置信息显示城市下拉框,由于multimap不支持直接把所有的key的集合读取出来,所以需要遍历,把不同的城市信息显示到m_wndCity下拉框中。
LRESULT CMobileRadioView::OnComboCityCbnSelChange(WORD wNotifyCode, WORD wID, HWND hWndCtl){ CString str;int sel = m_wndCity.GetCurSel(); m_wndCity.GetLBText(sel, str); m_wndStation.ResetContent(); std::string city = CT2CA(str);unsigned int i = 0;for(CityStationMap::iterator it=cityStationMap.find(city); it!=cityStationMap.end() && i<cityStationMap.count(city); ++it,++i) { CString s = it->second->Name.c_str();int index = m_wndStation.AddString(s); m_wndStation.SetItemData(index, it->second->Id); }return 0;}
当城市下拉框的选择发生改变时,根据multimap的信息显示该城市下的电台信息。
LRESULT CMobileRadioView::OnComboStationCbnSelChange(WORD wNotifyCode, WORD wID, HWND hWndCtl){int sel = m_wndStation.GetCurSel();int id = (int)m_wndStation.GetItemData(sel);//CString image = (GetCurrentPath() + "Image\\" + stationMap[id]->Image).c_str(); //m_wndPic.SetBitmap(LoadBitmap(NULL, image));m_spWMPPlayer->put_URL(CComBSTR(stationMap[id]->Stream.c_str()));return 0;}
当电台下拉框的选择发生改变时,根据map的信息使用Windows Media Player控件播放该电台。
下一次讲述程序如何支持accelerometer(重力感应器)。
关于Mobile Radio - Internet Radio Software for Windows Mobile项目
目前(2009年9月份)这个项目基本功能已经完成,只是界面方面需要改进,提高用户体验。我把项目host到Mobile Radio - Internet Radio Software for Windows Mobile了,我会持续改进,主要是提高用户体验方面。
需要了解项目最新动态,可以访问Mobile Radio - Internet Radio Software for Windows Mobile 和我的Blog精简开发 无线生活。
源代码: 查看Mobile Radio最新源代码
环境:VS2008 + WM 6 professional SDK + WTL 8.1 + TinyXML
转载于:https://www.cnblogs.com/procoder/archive/2009/07/21/Windows_Mobile_TinyXML.html
Windows Mobile和Wince下使用TinyXML进行Native C++的开发相关推荐
- Windows Mobile和Wince(Windows Embedded CE)的字符集问题
背景 开发过Windows Mobile和Wince(Windows Embedded CE)的开发者,特别是Native C++开发者,或多或少都遇到过ANSI字符集和Unicode字符集的转换问题 ...
- 一文讲清楚Windows Mobile和Wince(Windows Embedded CE)的字符集问题
背景 开发过Windows Mobile和Wince(Windows Embedded CE)的开发者,特别是Native C++开发者,或多或少都遇到过ANSI字符集和Unicode字符集的转换问题 ...
- Windows Mobile使用Shared Memory(共享内存)进行IPC(进程间通信)的开发
背景 在Unix-like系统进行IPC(Inter-process communication)通信,Shared memory是效率最高的,我称之为IPC的王中王. 简介 本文讲述在Windows ...
- Windows Mobile和Wince(Windows Embedded CE )下使用.NET Compact Framework下注册表导出工具的开发...
简介 本文讲述了.NET Compact Framework下的注册表开发的基本概念,介绍在Windows Mobile和Wince下操作注册表的工具,同时使用C#实现了一个注册表导出工具. 背景 W ...
- 在Windows Mobile和Wince(Windows Embedded CE)下进行Native C++开发,如何取出当前执行文件的路径和调用模块的路径...
在Windows Mobile和Wince(Windows Embedded CE)下进行Native C++开发,有时候需要取出当前执行文件的路径,或者调用模块的路径信息,例如在相对路径下存放了配置 ...
- 在Windows Mobile和Wince(Windows Embedded CE)下进行WTL开发,如何加入超链接(HyperLink)
本文讲述在Windows Mobile和Wince(Windows Embedded CE)下进行WTL开发,如何加入超链接(HyperLink)的支持. 步骤如下: 1.#include <a ...
- 开发Windows Mobile和Wince(Windows Embedded CE)的部署项目(Deploy Project)时,如何修改注册表...
由于fele问我一个问题:在进行Windows Mobile部署项目的开发,也就是打安装包,如何修改注册表?我把自己的经验blog下来. 1.打开Deploy项目的注册表编辑器,如下图 2.根据需求修 ...
- Windows Mobile 6.0下实现自绘多种状态按钮(Win32) 续
这篇文章是以前的补充: http://www.cnblogs.com/wangkewei/archive/2009/02/24/1397490.html 放在首页是想借助各位从事Windows Mob ...
- 在Windows mobile 5.0下操作INI文件
对于Windows mobile 5.0来说没有像window那样操作INI文件的API,所以只能自已来实现.其实操作INI文件就是操作普通的文本文件,只是要麻烦一些.以下是我实现的一些常用的操作的函 ...
最新文章
- plsql developer导出csv乱码问题
- python编的俄罗斯方块游戏_手把手制作Python小游戏:俄罗斯方块(一)
- Split-Bregman迭代方式
- knn算法(分类)-机器学习
- swagger的使用方式
- pause容器作用_容器编排之战(四)连载
- class matplotlib.figure.Figure
- RxJS的另外四种实现方式(一)——代码最小的库
- script-百度换肤效果
- linux中判断语句,Linux--shel的if判断语句--05
- 九小时九个人九扇门(01背包)
- 01-移植AZPR SoC到Xilinx Aritix7 FPGA
- 【备忘】年薪50万2017年最新北风网Spark2.0从入门到精通教程
- python3实现国密SM4算法
- 一文带你搞定AOP切面
- 二维向量的叉乘判断凹凸多边形
- API调用,API传参,面向对接开发,你真的会写接口文档吗?
- msys64安装使用
- 体系结构14_控制相关的动态解决技术
- 财务大数据课程要学python吗_大数据不用学python吗?
热门文章
- leetcode-16-最接近的三数之和
- 【模板篇】数论大杂烩~
- SQL记录-PLSQL异常
- private static final long serialVersionUID = 1L;详解
- 一步一步写算法(之克鲁斯卡尔算法 中)
- 从零开始学C++之继承(一):公有/私有/保护继承、overload/overwrite/override之间的区别...
- SQL:waitfor的使用
- 什么是技术,技术是什么
- sjms-2 创建型模式
- Docker 与 DevOps自动化部署实践