参考:http://blog.csdn.net/clever101/article/details/5334369
参考:http://www.programgo.com/article/73711496638/
近期在学习xml,偶然得知tinyXml这个东西,想尽快上手。
(1)在http://sourceforge.net/projects/tinyxml/下载TinyXml的官方例子
(2)新建空的控制台程序,将(1)中下载的官方例子中的
tinyxml.h
tinystr.h
tinystr.cpp
tinyxml.cpp
tinyxmlparser.cpp
tinyxmlerror.cpp
包含到当前工程中。
(3)新建createXML.cpp.cpp文件
包含头文件 #include "tinyxml.h"
复制如下代码
#include <iostream>
#include <tchar.h>
#include "tinyxml.h"
/*!
* /brief 创建xml文件。
*
* /param XmlFile xml文件全路径。
* /return 是否成功。true为成功,false表示失败。
*/
bool CreateXml(std::string XmlFile)
{
// 定义一个TiXmlDocument类指针
TiXmlDocument *pDoc = new TiXmlDocument;
if (NULL==pDoc)
{
return false;
}
TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(_T("1.0"),_T(""),_T(""));
if (NULL==pDeclaration)
{
return false;
}
pDoc->LinkEndChild(pDeclaration);
// 生成一个根节点:MyApp
TiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));
if (NULL==pRootEle)
{
return false;
}
pDoc->LinkEndChild(pRootEle);
// 生成子节点:Messages
TiXmlElement *pMsg = new TiXmlElement(_T("Messages"));
if (NULL==pMsg)
{
return false;
}
pRootEle->LinkEndChild(pMsg);
// 生成子节点:Welcome
TiXmlElement *pWelcome = new TiXmlElement(_T("Welcome"));
if (NULL==pWelcome)
{
return false;
}
pMsg->LinkEndChild(pWelcome);
// 设置Welcome节点的值
std::string strValue = _T("Welcome to MyApp");
TiXmlText *pWelcomeValue = new TiXmlText(strValue.c_str());
pWelcome->LinkEndChild(pWelcomeValue);
// 生成子节点:Farewell
TiXmlElement *pFarewell = new TiXmlElement(_T("Farewell"));
if (NULL==pFarewell)
{
return false;
}
pMsg->LinkEndChild(pFarewell);
// 设置Farewell节点的值
strValue = _T("Thank you for using MyApp");
TiXmlText *pFarewellValue = new TiXmlText(strValue.c_str());
pFarewell->LinkEndChild(pFarewellValue);
// 生成子节点:Windows
TiXmlElement *pWindows = new TiXmlElement(_T("Windows"));
if (NULL==pWindows)
{
return false;
}
pRootEle->LinkEndChild(pWindows);
// 生成子节点:Window
TiXmlElement *pWindow = new TiXmlElement(_T("Window"));
if (NULL==pWindow)
{
return false;
}
pWindows->LinkEndChild(pWindow);
// 设置节点Window的值
pWindow->SetAttribute(_T("name"),_T("MainFrame"));
pWindow->SetAttribute(_T("x"),_T("5"));
pWindow->SetAttribute(_T("y"),_T("15"));
pWindow->SetAttribute(_T("w"),_T("400"));
pWindow->SetAttribute(_T("h"),_T("250"));
// 生成子节点:Window
TiXmlElement *pConnection = new TiXmlElement(_T("Connection"));
if (NULL==pConnection)
{
return false;
}
pRootEle->LinkEndChild(pConnection);
// 设置节点Connection的值
pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));
pConnection->SetAttribute(_T("timeout"),_T("123.456000"));
pDoc->SaveFile(XmlFile.c_str());
return true;
}
/*!
* /brief 打印xml文件。
*
* /param XmlFile xml文件全路径。
* /return 是否成功。true为成功,false表示失败。
*/
bool PaintXml(std::string XmlFile)
{
// 定义一个TiXmlDocument类指针
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)
{
return false;
}
pDoc->LoadFile(XmlFile.c_str());
pDoc->Print();
return true;
}
int main()
{
std::string XmlFile = _T("myTestTinyXml");
if (CreateXml(XmlFile))
{
PaintXml(XmlFile);
}
return 0;
}
(4)编译链接运行即可!
本文的代码链接:http://download.csdn.net/detail/fan0920/9617659