-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIBXML.cpp
155 lines (102 loc) · 3.1 KB
/
LIBXML.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "LIBXML.h"
#include <libxml/xpath.h>
#include <libxml/tree.h>
///////////////// CLIBXML_DOM_Attribute /////////////////
CLIBXML_DOM_Attribute::CLIBXML_DOM_Attribute( xmlNodePtr pNode ) : m_pNode( pNode )
{
}
CLIBXML_DOM_Attribute::~CLIBXML_DOM_Attribute()
{
}
bool CLIBXML_DOM_Attribute::get( const std::string &stName, std::string &stValue )
{
xmlChar* xchResult = xmlGetProp( m_pNode, (const xmlChar*) stName.c_str() );
if( xchResult == NULL )
return false;
stValue = ( char* ) xchResult;
return true;
}
CLIBXML_DOM_Attribute::map CLIBXML_DOM_Attribute::ToMap( void )
{
xmlAttr* Attr = m_pNode->properties;
CLIBXML_DOM_Attribute::map map;
while( Attr != 0 )
{
std::string stValue;
std::string stName = ( char* )Attr->name;
get( stName, stValue );
map[ stName ] = stValue;
Attr = Attr->next;
}
return map;
}
///////////////// CLIBXML_DOM_Attribute /////////////////
///////////////// CLIBXML_DOM_Node /////////////////
CLIBXML_DOM_Node::CLIBXML_DOM_Node( xmlNodePtr pNode ) : m_pNode( pNode )
{
}
CLIBXML_DOM_Node::~CLIBXML_DOM_Node()
{
}
DOMNodePtr CLIBXML_DOM_Node::getFirstChildNode()
{
if( !m_pNode->children )
return DOMNodePtr();
xmlNodePtr pNode = m_pNode->children->next;
if( pNode == NULL )
return DOMNodePtr();
return DOMNodePtr( new CLIBXML_DOM_Node( pNode ) );
}
DOMNodePtr CLIBXML_DOM_Node::getNextSiblingNode()
{
if( !m_pNode->next )
return DOMNodePtr();
xmlNodePtr pNode = m_pNode->next->next;
if( pNode == NULL )
return DOMNodePtr();
return DOMNodePtr( new CLIBXML_DOM_Node( pNode ) );
}
xmlNodePtr CLIBXML_DOM_Node::get()
{
return m_pNode;
}
///////////////// CLIBXML_DOM_Node /////////////////
///////////////// CLIBXML_DOM_Document /////////////////
CLIBXML_DOM_Document::CLIBXML_DOM_Document() : m_pDoc( NULL )
{
}
CLIBXML_DOM_Document::~CLIBXML_DOM_Document()
{
}
void CLIBXML_DOM_Document::addNode( xmlNodePtr _parent, xmlNodePtr _child )
{
xmlAddChild(_parent, _child);
}
bool CLIBXML_DOM_Document::load( const std::string &stFile )
{
m_pDoc = xmlParseFile( stFile.c_str() );
return m_pDoc != NULL;
}
int CLIBXML_DOM_Document::save( const std::string &stFile )
{
return xmlSaveFile( stFile.c_str(), m_pDoc );
}
DOMNodePtr CLIBXML_DOM_Document::getNode( const std::string &stXPath )
{
xmlXPathContextPtr context = xmlXPathNewContext( m_pDoc );
xmlXPathObjectPtr xpathObject = xmlXPathEval( (xmlChar*)stXPath.c_str(), context );
if( xpathObject == NULL || xpathObject->nodesetval == NULL || xpathObject->nodesetval->nodeNr != 1 )
{
xmlXPathFreeContext( context );
return DOMNodePtr();
}
DOMNodePtr result( new CLIBXML_DOM_Node( xpathObject->nodesetval->nodeTab[0] ) );
xmlXPathFreeContext( context );
xmlXPathFreeObject( xpathObject );
return result;
}
xmlDocPtr CLIBXML_DOM_Document::get()
{
return m_pDoc;
}
///////////////// CLIBXML_DOM_Document /////////////////