c.xml 如下
/
<?xml version="1.0" encoding="x-cp20936"?>
<system>
<fontname>ЫЮЬх</fontname>
<tinyfontsize size="12"/>
<backfile>20080202190047182.jpg</backfile>
<window minWidth="640" minHeight="480"/>
<adjuststatic>
<test1> this1 </test1>
<test2> this2 </test2>
</adjuststatic>
</system>
b.xml 如下
/
<?xml version="1.0"?>
<account>
<name>pxN/s84Vb5kxMzQzNjM4</name>
<uniqueid>OX7q6n8PPO0xNg==</uniqueid>
<imstatus>Q8VULnOFagk=</imstatus>
<loginType>MA==</loginType>
<logoindex>0</logoindex>
<logourl>http://downhdlogo.yy.com/hdlogo/100100/100/100/32/1849328616/u18493286165K0gQUM.png</logourl>
</account>
<account>
<name>123</name>
<uniqueid>OX7q6n8PPO0xNg==6</uniqueid>
<imstatus>Q8VULnOFagk=6</imstatus>
<loginType>MA==6</loginType>
<test>
<test1> comehere </test1>
<test2> love有 </test2>
</test>
<logoindex>06</logoindex>
<logourl>http://downhdlogo.yy.com/hdlogo/100100/100/100/32/1849328616/u18493286165K0gQUM.png6</logourl>
</account>
/
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <cassert>
#include <Windows.h>
#include "tinyxml.h"
using namespace std;
void getdatafromxml();
void ReadData();
const char* kFileName = "b.xml";
int main() {
// WriteData();
//ReadData();
getdatafromxml();
getchar();
return 0;
}
void getdatafromxml()
{
char xmlFile[256] = "c.xml";
TiXmlDocument doc(xmlFile);
bool loadOkay = doc.LoadFile();
if (!loadOkay)
{
cout<<"Load failed!"<<endl;
}
setlocale( LC_CTYPE, "chs" );
TiXmlNode* node = 0;
TiXmlNode*nextnode=0;
TiXmlElement* todoElement = 0;
node = doc.FirstChild( "system" );
if (0 == node)
return;
if (nextnode=node->FirstChild("fontname"))
{
todoElement=nextnode->ToElement();
string strGet = todoElement->GetText();
cout<<strGet<<endl;
}
if (nextnode=node->FirstChild("tinyfontsize"))
{
todoElement=nextnode->ToElement();
int x;
todoElement->QueryIntAttribute("size",&x); //有属性的元素,使用元素类的对象函数,取得属性值
cout<<x<<endl;
}
if (nextnode = node->FirstChild("backfile"))
{
todoElement = nextnode->ToElement(); //节点对象转换成元素对象,然后通过元素对象调用函数,获取元素值
string str = todoElement->GetText();
cout<<str<<endl;
/* sysEntry.background=new ImageWrap;
if(false==sysEntry.background->loadFile(strChange.doChange(todoElement->GetText()).c_str()))
{
delete sysEntry.background;
sysEntry.background=0;
}*/
}
if (nextnode = node->FirstChild("window"))
{
todoElement = nextnode->ToElement();
// sysEntry.minWindowSize.cx=640;
//sysEntry.minWindowSize.cy=480;
int value;
todoElement->QueryIntAttribute("minWidth",&value);
cout<<value<<endl;
todoElement->QueryIntAttribute("minHeight",&value);
cout<<value<<endl;
}
if (nextnode=node->FirstChild("adjuststatic"))
{
TiXmlNode* childNode = 0;
if (childNode = nextnode->FirstChild("test1")) //向下寻找节点
{
// childNode=nextnode->FirstChild("test1");
todoElement = childNode->ToElement();
string str = todoElement->GetText();
cout<<str<<endl;
childNode = nextnode->FirstChild("test2");
todoElement = childNode->ToElement();
str = todoElement->GetText();
cout<<str<<endl;
}
}
}
void ReadData() {
TiXmlDocument xdoc;
if (!xdoc.LoadFile(kFileName)) {
return;
}
// xdoc.Print();
TiXmlElement* filexml = xdoc.RootElement();
if (filexml == NULL) {
return;
}
while (filexml != NULL) {
TiXmlElement* xname = filexml->FirstChildElement("name");
std::string name = xname->GetText();
cout<<name<<endl;
TiXmlElement* uniqueid = filexml->FirstChildElement("uniqueid");
std::string uni = uniqueid->GetText();
cout<<uni<<endl;
TiXmlElement* imstatus = filexml->FirstChildElement("imstatus");
std::string imst = imstatus->GetText();
cout<<imst<<endl;
TiXmlElement* loginType = filexml->FirstChildElement("loginType");
std::string login = loginType->GetText();
cout<<login<<endl;
TiXmlNode* childnode = 0;
TiXmlNode* nextnode = 0;
//if(nextnode = childnode->FirstChild("test"))
//{
// nextnode = childnode->FirstChild("test1");
// filexml = nextnode->ToElement();
// string str = filexml->GetText();
// cout<<str<<endl;
// nextnode = childnode->FirstChild("test2");
// filexml = nextnode->ToElement();
// str = filexml->GetText();
// cout<<str<<endl;
//}
TiXmlElement* logoindex = filexml->FirstChildElement("logoindex");
std::string loginde = logoindex->GetText();
cout<<loginde<<endl;
TiXmlElement* logourl = filexml->FirstChildElement("logourl");
std::string loginu = logourl->GetText();
cout<<loginu<<endl;
filexml = filexml->NextSiblingElement();
}
}