使用TinyXml對xml文件進行操作

  • 簡介

讀取和設置xml配置文件是最常用的操作,TinyXML是一個開源的解析XML的C++解析庫,它是目前非常流行的一款基於DOM模型的XML解析器,簡單易用且小巧玲瓏,非常適合存儲簡單數據,配置文件,對象序列化等數據量不是很大的操作。

  下載TinyXML的網址:http://www.grinninglizard.com/tinyxml/

  使用TinyXML只需要將其中的6個文件拷貝到項目中就可以直接使用了,這六個文件是:tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。

  • 讀取xml
<School name="北大">  
    <Class name = "C++">         
        <Student name="tinyxml" number="123">  
            <email>[email protected]</email>  
            <address>中國</address>           
        </Student>          
        <Student name="haha" number="456">  
            <email>[email protected]</email>  
            <address>美國</address>           
        </Student>         
    </Class>     
</School>
#include "tinyxml.h"
#include <iostream>
#include <string>
#include <windows.h>

void readXml() 
{
	using namespace std;
	const char * xmlFile = "a.xml";
	TiXmlDocument doc;

	doc.LoadFile(xmlFile);
	TiXmlElement* rootElement = doc.RootElement();  //School元素  
	TiXmlElement* classElement = rootElement->FirstChildElement();  // Class元素
	TiXmlElement* studentElement = classElement->FirstChildElement();  //Students  

	for (; studentElement != NULL; studentElement = studentElement->NextSiblingElement()) 
	{
		TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute();  //獲得student的name屬性  
		for (; attributeOfStudent != NULL; attributeOfStudent = attributeOfStudent->Next()) 
		{
			cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl;
		}

		TiXmlElement* studentContactElement = studentElement->FirstChildElement();//獲得student的第一個聯繫方式 

		for (; studentContactElement != NULL; studentContactElement = studentContactElement->NextSiblingElement()) 
		{
			string contactType = studentContactElement->Value();
			//string contactValue = studentContactElement->GetText();
			string contactValue = UTF8_To_string(studentContactElement->GetText());
			cout << contactType << " : " << contactValue << std::endl;
		}
	}
}


//vs輸出的是gb2312的格式,而xml當前輸出的是utf-8,如果不轉爲gb2312會出現中文亂碼
//UTF8轉gb2312
std::string UTF8_To_string(const std::string & str)
{
	int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

	wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然會出現尾巴  
	memset(pwBuf, 0, nwLen * 2 + 2);

	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

	int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

	char * pBuf = new char[nLen + 1];
	memset(pBuf, 0, nLen + 1);

	WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

	std::string retStr = pBuf;

	delete[]pBuf;
	delete[]pwBuf;

	pBuf = NULL;
	pwBuf = NULL;

	return retStr;
}
  • 生成xml
  • void writeXml() 
    {
    	using namespace std;
    	const char *xmlFile = "write.xml";
    
    	TiXmlDocument doc;
    	TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", "");
    	TiXmlElement * schoolElement = new TiXmlElement("School");
    	TiXmlElement * classElement = new TiXmlElement("Class");
    	classElement->SetAttribute("name", "C++");
    
    	TiXmlElement * stu1Element = new TiXmlElement("Student");
    	stu1Element->SetAttribute("name", "tinyxml");
    	stu1Element->SetAttribute("number", "123");
    	TiXmlElement * stu1EmailElement = new TiXmlElement("email");
    	stu1EmailElement->LinkEndChild(new TiXmlText("[email protected]"));
    	TiXmlElement * stu1AddressElement = new TiXmlElement("address");
    	stu1AddressElement->LinkEndChild(new TiXmlText("中國"));
    	stu1Element->LinkEndChild(stu1EmailElement);
    	stu1Element->LinkEndChild(stu1AddressElement);
    
    	TiXmlElement * stu2Element = new TiXmlElement("Student");
    	stu2Element->SetAttribute("name", "jsoncpp");
    	stu2Element->SetAttribute("number", "456");
    	TiXmlElement * stu2EmailElement = new TiXmlElement("email");
    	stu2EmailElement->LinkEndChild(new TiXmlText("[email protected]"));
    	TiXmlElement * stu2AddressElement = new TiXmlElement("address");
    	stu2AddressElement->LinkEndChild(new TiXmlText("美國"));
    	stu2Element->LinkEndChild(stu2EmailElement);
    	stu2Element->LinkEndChild(stu2AddressElement);
    
    	classElement->LinkEndChild(stu1Element);
    	classElement->LinkEndChild(stu2Element);
    	schoolElement->LinkEndChild(classElement);
    
    	doc.LinkEndChild(decl);
    	doc.LinkEndChild(schoolElement);
    	doc.SaveFile(xmlFile);
    }

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章