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開發
轉換Json到XML的JavaScript實現
?
簡介
XML已經成為流行的數據保存和交換的格式,本文講述如何使用TinyXML在Windows Mobile下進行XML的開發。TinyXML是簡單,輕裝,跨平臺的原生C++ xml解釋器,可以十分簡便的整合到其他系統中。同時TinyXML提供完整的在線文檔,方便開發和使用。目前,由于其簡便性和穩定性,使用ZLib license(可以用于開源和商業)等原因,TinyXML已經廣泛被用于開源社區和商業系統中。
關于更多TinyXML的介紹請看下面鏈接:
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 );
//#endif
return 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 -> Station
typedef 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++的开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 52. N-Queens II
- 下一篇: 开发Windows Mobile和Win