-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXML.cpp
69 lines (60 loc) · 1.34 KB
/
XML.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
#include "XML.h"
#include <sstream>
#include <iomanip>
using namespace std;
XML::XML( const string &tag ):
dbg("XML"),
tag( tag )
{
}
XML::~XML()
{
}
void XML::addNode( const XML &node )
{
value.clear();
nodes.push_back( node );
}
string XML::str( int depth, string transform )
{
std::ostringstream oss;
if (depth == 0 )
{
oss << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl << endl;
if ( transform.size() > 0 )
{
oss << "<?xml-stylesheet type=\"text/xsl\" href=\"" <<
transform << "\"?>" << endl;
}
oss << endl;
}
oss << string(depth, ' ' );
oss << "<" << tag;
// Add attributes
for ( map<std::string, std::string>::iterator iter = attributes.begin();
iter != attributes.end();
++iter )
{
oss << " " << iter->first << "=\"" << iter->second << "\"";
}
if ( value.size() != 0 )
{
oss << ">" << value << "</" << tag << ">" << endl;
}
else if ( nodes.size() != 0 )
{
oss << ">" << endl;
for ( vector< XML >::iterator iter = nodes.begin();
iter != nodes.end();
++iter )
{
oss << (iter)->str( depth + 1 );
}
oss << "</" << tag << ">" << endl;
}
else // No value or no subnodes
{
oss << "/>" << endl;
}
return oss.str();
}