zoukankan      html  css  js  c++  java
  • C++ 读取XML文件(tinyXML库的应用)

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

    Skin.xml文档内容如下:

    <UI>
    	<Image name="banner" x="0" y= "0"  width="900" height="40" img_src="imguianner.jpg" />
    	<Image name="logo" x="5" y= "5"  width="32" height="32" img_src="imguilogo.png" />
    	<Image name="logo" x="863" y= "5"  width="32" height="32" img_src="imguiclose.png" />
    	<Button id="1001" x="540" y= "360"  width="48" height="48" img_normal="imguitn_start_normal.png"  img_hover="imguitn_start_hover.png" img_down="imguitn_start_normal.png" />
    	<Button id="1002" x="540" y= "480"  width="48" height="48" img_normal="imguitn_stop_normal.png"  img_hover="imguitn_stop_hover.png" img_down="imguitn_stop_normal.png" />
    	<Button id="1003" x="250" y= "5"  width="48" height="48" img_normal="imguitn_pre_normal.png"  img_hover="imguitn_pre_hover.png" img_down="imguitn_pre_normal.png" />
    	<Button id="1004" x="250" y= "845"  width="48" height="48" img_normal="imguitn_next_normal.png"  img_hover="imguitn_next_hover.png" img_down="imguitn_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();
    	}
    	
    	
    }
    

      

  • 相关阅读:
    实训9.4.前端:url、href、src,link和@import
    实训9.2.作业1.写一个10次循环,每次得到一个随机数,放进一个集合中,如果这个数已经存在集合中则跳过,最后打印集合中的数字.
    实训9.3. SQL——STRUCTURED QUERY LANGUAGE(结构化查询语言 )
    实训9.2.类集,Collection接口
    实训9.2.IDEA ——java编程语言开发的集成环境(集成开发工具)
    实训9.2. JDK——java语言的软件开发工具包(JAVA的运行环境(JVM+Java系统类库)和JAVA工具) 【java开发的核心】
    从键盘输入数据
    error
    ubuntu 14.04, Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='
    用Python徒手写线性回归
  • 原文地址:https://www.cnblogs.com/JczmDeveloper/p/3494640.html
Copyright © 2011-2022 走看看