一. 下载编译
到官网下载tinyxml_2_6_2.zip http://www.grinninglizard.com/tinyxml/
现在出了TinyXML2, 那里说2的效率会高一点
http://www.grinninglizard.com/tinyxml2/index.html
我下载的是tinyxml_2_6_2 编译需要VC2010, 由于我用的版本比2010低, 所以我自己重新创建一个静态库工程编译, 不使用VC2010编译.
编译步骤:
A. 使用VC2008/2005 创建一个Win32静态库工程, 把TinyXML中的tinystr.cpp;tinystr.h;tinyxml.cpp;tinyxml.h;tinyxmlerror.cpp;tinyxmlparser.cpp 共6个文件添加到静态库编程, 按提示修改错误(预编译头错误). 完成即可.
二. 最简单的使用
你懂的
- #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;
- }
#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