转载:http://my.oschina.net/Davy2010/blog/11454
经过两天的摸索,现在终于会用一点Tinyxml了,现将过程记下。
首先下载我自己整理的 Tinyxml 的六个通用文件(头文件和实现文件),下载地址:
http://www.vdisk.cn/down/index/5804602A5578
Step 1: 用 Vs 新建一个win32 console 工程。
Step 2: 在工程里添加进那六个文件(tinystr.h tinyxml.h 和 tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp).
Step 3: Xml文件内容,保存为"test.xml", 放在工程文件夹下。
<?xml version="1.0" encoding="GB18030" ?>
<Menus>
<Menu title="常用网址">
<item name="天下网" url="http://www.netskycn.com" id="1"/>
<item name="天下网生活论坛" url="http://life.netskycn.com" id="2"/>
<item name="csdn" url="http://www.csdn.net" id="3"/>
<item name="我的博客" url="http://blog.csdn.net/zhoufoxcn" id="4"/>
<item name="百度" url="http://www.baidu.com" id="5"/>
<item name="Google" url="http://www.google.cn" id="6"/>
<item name="微软" url="http://www.microsoft.com" id="7"/>
</Menu>
<Menu title="娱乐网址">
<item name="奇虎" url="http://www.qihoo.com" id="12"/>
<item name="网易" url="http://www.163.com" id="13"/>
<item name="天涯" url="http://www.tianya.cn" id="14"/>
</Menu>
<Menu title="安全网址">
<item name="360" url="http://www.safe360.com" id="15"/>
<item name="瑞星" url="http://www.rising.com.cn" id="16"/>
</Menu>
</Menus>
// XMLParse_test_3_using_LoadFile.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "tinyxml.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// step 1 : Load the xml file
string fileXml = "test2.xml";
TiXmlDocument doc( fileXml.c_str() );
doc.LoadFile();
// step 2 (optional) : // Print the file's content
doc.Print();
// step 3 : Parse the xml file
if ( !doc.Parse( fileXml.c_str() ) )
{
cout << doc.ErrorDesc() << endl;
}
// step 3.1 : Get the RootElement
const TiXmlElement *root = doc.RootElement();
// Testing 1
cout << endl << " The root element is : " << root->Value() << endl << endl;
// step 3.2 : Get RootElement's ChildElement
cout << "Menu : \n";
for ( const TiXmlNode *child = root->FirstChild(); child; child = child->NextSibling() )
{
// Read the ChildElement
if ( ( child->Type() == TiXmlNode::ELEMENT ) && ( !strcmp( child->Value(), "Menu" ) ) )
{
const TiXmlElement *child_menu = ( const TiXmlElement * ) child;
string url, id, name;
// Get the attribute_title
string attribute_title;
attribute_title = child_menu->Attribute("title");
// Testing 2
cout << " The title is : " << attribute_title << endl;
// Get child_child_element_item s
cout << " Item : \n";
for ( const TiXmlNode *child_child_item = child_menu->FirstChild();
child_child_item;
child_child_item = child_child_item->NextSibling() )
{
if ( child_child_item->Type() == TiXmlNode::ELEMENT )
{
const TiXmlElement *child_child_element_item = ( const TiXmlElement * )child_child_item;
if ( !strcmp( child_child_element_item->Value(), "item" ) )
{
name = child_child_element_item->Attribute( "name" );
url = child_child_element_item->Attribute( "url" );
id = child_child_element_item->Attribute( "id" );
}
}
cout << "\tname : " << name << endl
<< "\turl : " << url << endl
<< "\tid : " << id << endl << endl;
}
}
}
system("pause");
return 0;
}

浙公网安备 33010602011771号