TinyXML使用

6778 ワード

一. ダウンロードコンパイル
公式サイトでtinyxmlをダウンロードします。2_6_2.zip http://www.grinninglizard.com/tinyxml/ 
今TinyXML 2が出ました。 そこで2の効率が高いと言いました。
http://www.grinninglizard.com/tinyxml2/index.html
私がダウンロードしたのはtinyxmlです。2_6_2 コンパイルはVC 2010が必要です 私が使っているバージョンは2010より低いので、 だから私は自分でもう一度スタティックライブラリのプロジェクトのコンパイルを作成します。 VC 2010を使用しないでコンパイルします。
コンパイル手順:
A. VC 2008/2005を使用する Win 32静的ライブラリプロジェクトを作成します。 TinyXMLの中のtiystr.cppを。tisttr.h;tinyxml.cpp;ting xml.h;ティナxmlerror.cpp;ティナxmlparser.cpp 6つのファイルをスタティックライブラリに追加してプログラムします。 ヒントに従ってエラーを修正します。 完成すればいいです
二. 一番簡単に使う
承知してるでしょ
[cpp]view play copy print?
ヽoo。ツ "stdafx.h"  
  •   
  • ヽoo。ツ "../TinyXML/tinyxml.h"  
  • 〓〓include "../TinyXML/tinystr.h"  
  •   
  • 〓〓include   
  • ヽoo。ツ <ストリングス  
  • 〓〓〓pragma コメント(lib、「./Debug/tinyxml.lib」)  
  •   
  • book CreateXML File(std:string& strefile Name)  
  • {  
  •     try  
  •     {  
  •         // XMLの文書オブジェクトを作成します。  
  •         TiXml Dcument *xml Dcument = new TiXml Dcument();  
  •         // 根元素  
  •         TiXmlElement *rootElement = new TiXmlElement(「Books」)  
  •         // 文書オブジェクトに接続します。 根の要素として  
  •         xml Dcument->Link EndChild(rootElement);  
  •         // Book要素を作成します。  
  •         TiXmlElement *ブックレット = new TiXmlElement(「Book」)  
  •         // ルート要素に接続します 根の要素です。  
  •         rootElement->Link EdChild(book Element);  
  •         // Book要素の属性を設定します。 ここはIDです  
  •         book Element->>SetAttribute(「ID」) "1");  
  •         // Name元素とPrice元素を作成します。  
  •         TiXmlElement *name Element = new TiXmlElement("Name")  
  •         TiXmlElement *prceElement = new TiXmlElement(「Price」)  
  •         // 接続  
  •         book Element->Link EndChild(nameElement);  
  •         book Element->Link EndChild;  
  •         // Name元素とPrice元素の値を設定します。  
  •         TiXml Text *nameValue = new TiXml Text("ひまわりの花宝典")  
  •         name Element->Link EndChild;  
  •         TiXml Text *prceValue = new TiXml Text(「50.00」)  
  •         price Element->Link EndChild;  
  •         xml Dcument->SaveFile(strefileName.custr  // ファイルに保存  
  •     }  
  •     catch(…)  
  •     {  
  •         return false;  
  •     }  
  •     return true;  
  • }  
  •   
  • book ReadXML File(std:string& sz FileName)  
  • {  
  •     try  
  •     {  
  •         // XMLの文書オブジェクトを作成します。  
  •         TiXml Dcument *xml Dcument = new TiXml Dcument(szFileName.custr  
  •         // 解析  
  •         xml Dcument->LoadFile();  
  •         // ルート要素を取得  
  •         TiXmlElement *rootElement = xml Dcument->>RootElement()  
  •   
  •         // ルート要素名を出力  
  •         std:cout << rootElement->Value() << std::endl;  
  •         // 最初のノードを取得します。  
  •         TiXmlElement *firstElement = rootElement->First ChildElement()  
  •         // 最初のPersonのnameノードとageノードとID属性が得られる。  
  •         TiXmlElement *name Element = firstElement->First ChildElement()  
  •         TiXmlElement *prceElement = name Element->NextSiblingElement()  
  •         TiXml Attribute *IDAttribute = firstElement->>FirstAttribute();  
  •   
  •         // 出力  
  •         std:cout << name Element->First Child()->Value() << std::endl;  
  •         std:cout << price Element->First Child()->Value() << std::endl;  
  •         std:cout << IDAttribute->Value()<< std::endl;  
  •     }  
  •     catch (...)  
  •     {  
  •         return false;  
  •     }  
  •     return true;  
  • }  
  •   
  •   
  • 要点 _tman(int argc _TCHAR* argv[]  
  • {  
  •     std:sting fileName = "C:\test.xml";  
  •     CreateXML File(fileName);  
  •     ReadXMLFile(fileName);  
  •     return 0;  
  • )  
    #include "stdafx.h"
    
    #include "../TinyXML/tinyxml.h"
    #include "../TinyXML/tinystr.h"
    
    #include <iostream>
    #include <string>
    #pragma comment(lib,"../Debug/tinyxml.lib");
    
    bool CreateXMLFile(std::string& strFileName)
    {
    	try
    	{
    		//     XML     。
    		TiXmlDocument *xmlDocument = new TiXmlDocument();
    		//    。
    		TiXmlElement *rootElement = new TiXmlElement("Books");
    		//        ,      
    		xmlDocument->LinkEndChild(rootElement);
    		//     Book  .
    		TiXmlElement *bookElement = new TiXmlElement("Book");
    		//       ,          
    		rootElement->LinkEndChild(bookElement);
    		//   Book     ,    ID。
    		bookElement->SetAttribute("ID", "1");
    		//   Name   Price  
    		TiXmlElement *nameElement = new TiXmlElement("Name");
    		TiXmlElement *priceElement = new TiXmlElement("Price");
    		//   
    		bookElement->LinkEndChild(nameElement);
    		bookElement->LinkEndChild(priceElement);
    		//   Name   Price    。
    		TiXmlText *nameValue = new TiXmlText("    ");
    		nameElement->LinkEndChild(nameValue);
    		TiXmlText *priceValue = new TiXmlText("50.00");
    		priceElement->LinkEndChild(priceValue);
    		xmlDocument->SaveFile(strFileName.c_str());	//      
    	}
    	catch(...)
    	{
    		return false;
    	}
    	return true;
    }
    
    bool ReadXMLFile(std::string& szFileName)
    {
    	try
    	{
    		//     XML     。
    		TiXmlDocument *xmlDocument = new TiXmlDocument(szFileName.c_str());
    		//   
    		xmlDocument->LoadFile();
    		//      
    		TiXmlElement *rootElement = xmlDocument->RootElement();
    
    		//        
    		std::cout << rootElement->Value() << std::endl;
    		//        。
    		TiXmlElement *firstElement = rootElement->FirstChildElement();
    		//      Person name   age   ID  。
    		TiXmlElement *nameElement = firstElement->FirstChildElement();
    		TiXmlElement *priceElement = nameElement->NextSiblingElement();
    		TiXmlAttribute *IDAttribute = firstElement->FirstAttribute();
    
    		//   
    		std::cout << nameElement->FirstChild()->Value() << std::endl;
    		std::cout << priceElement->FirstChild()->Value() << std::endl;
    		std::cout << IDAttribute->Value()<< std::endl;
    	}
    	catch (...)
    	{
    		return false;
    	}
    	return true;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::string fileName = "C:\\test.xml";
    	CreateXMLFile(fileName);
    	ReadXMLFile(fileName);
    	return 0;
    }
    リソースのダウンロード先:
    http://download.csdn.net/detail/cay22/5088811