-
Notifications
You must be signed in to change notification settings - Fork 0
/
zork.cpp
99 lines (76 loc) · 2.19 KB
/
zork.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* zork.cpp
*
* Created on: Dec 6, 2014
* Author: hao
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"
#include "zork.h"
using namespace std;
using namespace rapidxml;
int main(int argc, char *argv[]){
if (argc < 2){
cout << "input xml missing"<<endl;
}else{
cout <<"number of arguments are: "<<argc<<endl;
cout << "input name :"<<argv[1]<<endl;
}
// use rapid xml to parse xml input file
rapidxml::file<> xmlFile(argv[1]);
rapidxml::xml_document<> doc;
doc.parse<0>(xmlFile.data());
// access the dom tree
cout << "Name of my first node is: " << doc.first_node()->name() << "\n";
xml_node<> *pRoot = doc.first_node();
//cout << "value size: "<< pRoot->value_size()<<endl; // size is 0
MapTraverse(pRoot);
return 0;
}
/*
* traverse the child nodes of map, like rooms, items, containers
*/
void MapTraverse(xml_node<> *pRoot){
// pNode initialized to the first child of map;
for(xml_node<> *pNode = pRoot->first_node(); pNode; pNode=pNode->next_sibling()){
string nodeName = pNode->name();
string nodeValue = pNode->value();
double valueSize = pNode->value_size();
cout << "node name: "<< nodeName<<endl;
if(valueSize !=0) cout<<"node value: "<<nodeValue<<endl;
if(nodeName == "room"){
RoomTraverse(pNode);
}else if(nodeName == "item"){
}else if(nodeName == "container"){
}else if(nodeName == "creature"){
}
}
return;
}
/*
* traverse the child nodes of room, like name, items, borders, containers
*/
void RoomTraverse(xml_node<> *pRoom){
// pNode initialized to the first child of room;
for(xml_node<> *pNode = pRoom->first_node(); pNode; pNode=pNode->next_sibling()){
string nodeName = pNode->name();
string nodeValue = pNode->value();
double valueSize = pNode->value_size();
cout << " node name: "<< nodeName<<endl;
if(valueSize !=0) cout<<" node value: "<<nodeValue<<endl;
if(nodeName == "name"){
}else if(nodeName == "description"){
}else if(nodeName == "item"){
}else if(nodeName == "trigger"){
}else if(nodeName == "border"){
}else if(nodeName == "container"){
}else if(nodeName == "creature"){
}
}
return;
}