Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
65 lines (56 loc) · 1.46 KB
/
Copy pathMain.cpp
File metadata and controls
65 lines (56 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "rapidxml.hpp"
using namespace std;
using namespace rapidxml;
void parse_node(xml_node<> *node = NULL);
int main(int argc, char **argv)
{
std::string fileName(".NET Framework 4.6.xml");
ifstream is(fileName);
stringstream ss;
ss << is.rdbuf();
std::string xml = ss.str();
char *pContent = new char[xml.length() + 1];
memset(pContent, 0, xml.length() + 1);
memcpy(pContent, xml.c_str(), xml.length());
xml_document<> doc; // character type defaults to char
doc.parse<0>(pContent); // 0 means default parse flags
xml_node<> *node = doc.first_node();
//parse document recursively
parse_node(node);
return 0;
}
void parse_node(xml_node<> *node )
{
if ( NULL == node)
{
return;
}
//parse self
cout << "************ name=" << node->name() << " ";
if ( node_data == node->type() )
{
cout << "value=" << node->value();
}
cout << endl;
//parse attributes
for (xml_attribute<> *attr = node->first_attribute();
attr; attr = attr->next_attribute())
{
cout << "attribute name=" << attr->name() << " ";
cout << "value=" << attr->value() << endl;
}
//parse brothers
if ( node->next_sibling() )
{
parse_node(node->next_sibling());
}
//parse children
if ( node->first_node())
{
parse_node(node->first_node());
}
}