#include <iostream>
#include "tinyxml.h"
//#include <windows.h>
#include <string>
#include <map>
#include <vector>
#include <assert.h>
using namespace std;
//http://www.grinninglizard.com/tinyxmldocs/tutorial0.html
struct TaskElement{
string value;
map<string,string> attributes;
string text;
};
void ParseXml( TiXmlNode* pParent, vector<TaskElement*>& taskVector )
{
if ( !pParent ) return;
assert(pParent->Type()==TiXmlNode::TINYXML_DOCUMENT);
TiXmlNode* pChild = pParent->FirstChild();
assert(pChild->Type()==TiXmlNode::TINYXML_ELEMENT);
assert(strcmp(pChild->Value(),"tasks")==0);
for ( pChild = pChild->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
TaskElement* element = new TaskElement();
assert((pChild->Type()==TiXmlNode::TINYXML_ELEMENT) && strcmp(pChild->Value(),"task")==0);
TiXmlElement* pElement = pChild->ToElement();
element->value = pElement->Value();
if (pElement->GetText())
element->text = pElement->GetText();
TiXmlAttribute* pAttrib = pElement->FirstAttribute();
while (pAttrib){
element->attributes[pAttrib->Name()] = pAttrib->Value();
pAttrib = pAttrib->Next();
}
taskVector.push_back(element);
}
}
void main(int argc, char** argv)
{
cout << "hello,world" << endl;
char* xmlStr = "<nodes><node>1</node><node>2</node></nodes>";
TiXmlDocument doc;
doc.Parse(xmlStr);
doc.SaveFile("tt.xml");
TiXmlDocument docx;
if ( ! docx.LoadFile("tt.xml") )
cout << "load xml file failed." << endl;
TiXmlDocument doc2;
TiXmlElement* root = new TiXmlElement("nodes");
TiXmlElement* element = new TiXmlElement("node");
element->SetAttribute("attr1", "aaaa");
element->SetAttribute("attr2", "bbbb");
TiXmlText* text = new TiXmlText("111");
element->LinkEndChild(text);
root->LinkEndChild(element);
TiXmlElement* element2 = new TiXmlElement("desc");
element2->LinkEndChild(new TiXmlText("its a test node"));
root->LinkEndChild(element2);
doc2.LinkEndChild(root);
doc2.SaveFile("tt2.xml");
//save to std::string
TiXmlPrinter printer;
doc2.Accept(&printer);
string saveXml = printer.CStr();
// <nodes>
// <node attr1="aaaa" attr2="bbbb">111</node>
// <desc>its a test node</desc>
// </nodes>
char* taskXml = "<tasks><task jobid=\"1\" stepid=\"1\" taskid=\"1\" status=\"finish\"/><task jobid=\"1\" stepid=\"2\" taskid=\"2\" status=\"stop\"/></tasks>";
TiXmlDocument xmlDoc;
xmlDoc.Parse(taskXml);
vector<TaskElement*> taskVector;
ParseXml(&xmlDoc, taskVector);
}
tinyxml下载:http://sourceforge.net/projects/tinyxml/
[dongsong@localhost tinyxml]$ tree -a
.
├── tinyxml
│?? ├── changes.txt
│?? ├── demotest.xml
│?? ├── Makefile
│?? ├── readme.txt
│?? ├── test5.xml
│?? ├── test6.xml
│?? ├── test7.xml
│?? ├── textfile.txt
│?? ├── tinystr.cpp
│?? ├── tinystr.h
│?? ├── tinyxml.cpp
│?? ├── tinyxmlerror.cpp
│?? ├── tinyxml.h
│?? ├── tinyxml_lib.vcxproj
│?? ├── tinyxmlparser.cpp
│?? ├── tinyxml.sln
│?? ├── tinyxmlSTL.vcxproj
│?? ├── tinyXmlTestSTL.vcxproj
│?? ├── tinyXmlTest.vcxproj
│?? ├── utf8test.gif
│?? ├── utf8testout.xml
│?? ├── utf8testverify.xml
│?? ├── utf8test.xml
│?? └── xmltest.cpp
└── xmlparser.cpp
1 directory, 25 files
[dongsong@localhost tinyxml]$ cd tinyxml/
[dongsong@localhost tinyxml]$ make
g++ -c -Wall -Wno-unknown-pragmas -Wno-format -O3 tinyxml.cpp -o tinyxml.o
g++ -c -Wall -Wno-unknown-pragmas -Wno-format -O3 tinyxmlparser.cpp -o tinyxmlparser.o
g++ -c -Wall -Wno-unknown-pragmas -Wno-format -O3 xmltest.cpp -o xmltest.o
g++ -c -Wall -Wno-unknown-pragmas -Wno-format -O3 tinyxmlerror.cpp -o tinyxmlerror.o
g++ -c -Wall -Wno-unknown-pragmas -Wno-format -O3 tinystr.cpp -o tinystr.o
g++ -o xmltest tinyxml.o tinyxmlparser.o xmltest.o tinyxmlerror.o tinystr.o
[dongsong@localhost tinyxml]$ cd -
/home/dongsong/c++_study/tinyxml
[dongsong@localhost tinyxml]$ g++ -o xmlparser xmlparser.cpp -I./tinyxml tinyxml/tinystr.o tinyxml/tinyxml.o tinyxml/tinyxmlerror.o tinyxml/tinyxmlparser.o
[dongsong@localhost tinyxml]$ ./xmlparser
hello,world