00001
00002
00003 #include "xmlnode.h"
00004 #include "xmlnamespace.h"
00005
00006
00007 namespace xml {
00008
00009
00011
00013 CXMLAttribute::CXMLAttribute(void) {
00014 m_name = std::string();
00015 m_value = std::string();
00016 }
00017
00018
00020
00022 CXMLAttribute::CXMLAttribute(xmlAttr *attribute) {
00023 if(attribute->name != NULL) {
00024 m_name = std::string((char*)attribute->name);
00025 }
00026 if(attribute->children != NULL && attribute->children->content != NULL) {
00027 m_value = std::string((char*)attribute->children->content);
00028 }
00029 }
00030
00031
00033
00035 CXMLAttribute::~CXMLAttribute(void) {
00036 }
00037
00038
00040
00042 void CXMLAttribute::print(void) {
00043 printf("XMLAttribut: %s: %s\n", m_name.c_str(), m_value.c_str());
00044 }
00045
00046
00048
00050 CXMLAttributeList::CXMLAttributeList(void) {
00051 m_attList = std::vector<CXMLAttribute>();
00052 }
00053
00054
00056
00058 CXMLAttributeList::~CXMLAttributeList(void) {
00059 }
00060
00061
00063
00065 int CXMLAttributeList::getSize(void) {
00066 return (int)m_attList.size();
00067 }
00068
00069
00071
00073 void CXMLAttributeList::add(CXMLAttribute element) {
00074 m_attList.push_back(element);
00075 }
00076
00077
00079
00081 CXMLAttribute CXMLAttributeList::get(int index) {
00082 return m_attList.at(index);
00083 }
00084
00085
00087
00089 void CXMLAttributeList::clear(void) {
00090 m_attList.clear();
00091 }
00092
00093
00095
00097 CXMLNode::CXMLNode(void) {
00098 this->m_node = NULL;
00099 }
00100
00101
00103
00105 CXMLNode::CXMLNode(xmlNodePtr node) {
00106 this->m_node = node;
00107 }
00108
00109
00111
00113 CXMLNode::~CXMLNode(void) {
00114
00115
00116
00117
00118
00119
00120 }
00121
00122
00124
00126 bool CXMLNode::getName(std::string *destination) {
00127 if(this->m_node) {
00128 if(this->m_node->name) {
00129 *destination = std::string((char*)this->m_node->name);
00130 return true;
00131 }
00132 }
00133 return false;
00134 }
00135
00136
00138
00140 int CXMLNode::getType(void) {
00141 return this->m_node->type;
00142 }
00143
00144
00146
00148 bool CXMLNode::getFirstChildNode(CXMLNode *destination) {
00149 xmlNode *nPtr;
00150
00151
00152 if(m_node != NULL) {
00153 if(m_node->children != NULL) {
00154
00155 nPtr = m_node->children;
00156
00157 while(nPtr != NULL) {
00158
00159
00160 if(nPtr->type == XML_ELEMENT_NODE) {
00161 *destination = CXMLNode(nPtr);
00162 return true;
00163 }
00164 nPtr = nPtr->next;
00165 }
00166 }
00167 }
00168 return false;
00169 }
00170
00171
00173
00175 bool CXMLNode::getNextSiblingNode(CXMLNode *destination) {
00176 xmlNode *nPtr;
00177
00178
00179 if(m_node != NULL) {
00180 if(m_node->next != NULL) {
00181
00182 nPtr = m_node->next;
00183
00184 while(nPtr != NULL) {
00185
00186
00187 if(nPtr->type == XML_ELEMENT_NODE) {
00188 *destination = CXMLNode(nPtr);
00189 return true;
00190 }
00191 nPtr = nPtr->next;
00192 }
00193 }
00194 }
00195 return false;
00196 }
00197
00198
00200
00202 bool CXMLNode::getNameSpace(CXMLNameSpace *destination) {
00203 if(this->m_node) {
00204 if(this->m_node->ns) {
00205 *destination = CXMLNameSpace(this->m_node->ns);
00206 return true;
00207 }
00208 }
00209 return false;
00210 }
00211
00212
00214
00216 bool CXMLNode::hasNameSpace(void) {
00217 if(this->m_node != NULL) {
00218 if(this->m_node->ns != NULL) {
00219 return true;
00220 }
00221 }
00222 return false;
00223 }
00224
00225
00227
00229 bool CXMLNode::getContent(std::string *destination) {
00230 if(this->m_node) {
00231 if(this->m_node->content) {
00232 *destination = std::string((char*)this->m_node->content);
00233 return true;
00234 }
00235 }
00236 return false;
00237 }
00238
00239
00241
00243 bool CXMLNode::getFirstDescendantByName(const std::string &name, CXMLNode *destination) {
00244 if(this->m_node) {
00245 xmlNodePtr result = CXMLNode::getFirstDescendantByNamexmlPtr(name, this->m_node);
00246 if(result) {
00247 *destination = CXMLNode(result);
00248 return true;
00249 }
00250 }
00251 return false;
00252 }
00253
00254
00256
00258 bool CXMLNode::getText(std::string *destination) {
00259 if(this->m_node) {
00260 if(this->m_node->children) {
00261 if(this->m_node->children->content) {
00262 *destination = std::string((char*)this->m_node->children->content);
00263 return true;
00264 }
00265 }
00266 }
00267 return false;
00268 }
00269
00270
00272
00274 bool CXMLNode::getAttributes(CXMLAttributeList *destination) {
00275 xmlAttr *aPtr;
00276
00277
00278 if(destination == NULL) {
00279 return false;
00280 }
00281
00282 if(m_node != NULL) {
00283 if(m_node->properties != NULL) {
00284
00285 aPtr = m_node->properties;
00286
00287 while(aPtr != NULL) {
00288 CXMLAttribute attr = CXMLAttribute(aPtr);
00289 destination->add(attr);
00290 aPtr = aPtr->next;
00291 }
00292 return true;
00293 }
00294 }
00295 return false;
00296 }
00297
00298
00300
00302 bool CXMLNode::getAttributeByName(const std::string &attrName, CXMLAttribute *destination) {
00303 xml::CXMLAttributeList attrList;
00304 xml::CXMLAttribute attr;
00305 int i;
00306
00307
00308
00309 if(getAttributes(&attrList)) {
00310 for(i = 0; i < attrList.getSize(); i++) {
00311 attr = attrList.get(i);
00312 if(attr.m_name == attrName) {
00313 *destination = attr;
00314 return true;
00315 }
00316 }
00317 }
00318 return false;
00319 }
00320
00321
00323
00325 void CXMLNode::print(bool recursive) {
00326
00327
00328
00329 xmlNodePtr nodePtr = NULL;
00330
00331
00332
00333 switch(this->m_node->type) {
00334 case 1: printf("XML Element Node: "); break;
00335 case 2: printf("XML Attribute Node: "); break;
00336 case 3: printf("XML Text Node: "); break;
00337 case 6: printf("XML Entity Node: "); break;
00338 case 8: printf("XML Comment Node: "); break;
00339 default: break;
00340 }
00341
00342 if(hasNameSpace()) {
00343
00344 printf("Name=\"%s::%s\";\n", this->m_node->ns->prefix, this->m_node->name);
00345 } else {
00346 printf("Name=\"%s\";\n", this->m_node->name);
00347 }
00348
00349 if(recursive) {
00350
00351 for(nodePtr = this->m_node->children; nodePtr != NULL; nodePtr = nodePtr->next) {
00352
00353 if(nodePtr->type != XML_TEXT_NODE) {
00354 CXMLNode(nodePtr).print(true);
00355 }
00356 }
00357 }
00358 }
00359
00360
00362
00364 xmlNodePtr CXMLNode::getFirstDescendantByNamexmlPtr(const std::string &name, xmlNodePtr current) {
00365
00366
00367
00368 xmlNodePtr nodePtr = NULL;
00369 xmlNodePtr recNode = NULL;
00370
00371
00372
00373
00374
00375 for(nodePtr = current->children; nodePtr != NULL; nodePtr = nodePtr->next) {
00376
00377 if(nodePtr->type != XML_TEXT_NODE) {
00378
00379 if(nodePtr->name) {
00380 if(std::string((char*)nodePtr->name) == name) {
00381 return nodePtr;
00382 }
00383 }
00384 }
00385 }
00386
00387
00388 for(nodePtr = current->children; nodePtr != NULL; nodePtr = nodePtr->next) {
00389
00390 if(nodePtr->type != XML_TEXT_NODE) {
00391 recNode = CXMLNode::getFirstDescendantByNamexmlPtr(name, nodePtr);
00392 if(recNode) {
00393 return recNode;
00394 }
00395 }
00396 }
00397
00398 return NULL;
00399 }
00400
00401
00403
00405 CXMLNodeSet::CXMLNodeSet(void) {
00406 this->m_nodeSet = NULL;
00407 }
00408
00409
00411
00413 CXMLNodeSet::CXMLNodeSet(xmlNodeSetPtr nodeSet) {
00414 this->m_nodeSet = nodeSet;
00415 }
00416
00417
00419
00421 CXMLNodeSet::CXMLNodeSet(xmlXPathObjectPtr object) {
00422 this->m_xpathObject = object;
00423 this->m_nodeSet = object->nodesetval;
00424 }
00425
00426
00428
00430 CXMLNodeSet::~CXMLNodeSet(void) {
00431 if(this->m_xpathObject) {
00432 xmlXPathFreeObject(this->m_xpathObject);
00433 }
00434
00435 }
00436
00437
00439
00441 int CXMLNodeSet::getNodeNum(void) {
00442 if(this->m_nodeSet) {
00443 return this->m_nodeSet->nodeNr;
00444 } else {
00445 return 0;
00446 }
00447 }
00448
00449
00451
00453 bool CXMLNodeSet::getNodeAt(int index, CXMLNode *destination) {
00454 if(index < this->getNodeNum()) {
00455 *destination = CXMLNode(this->m_nodeSet->nodeTab[index]);
00456 return true;
00457 }
00458 return false;
00459 }
00460
00461
00463
00465 void CXMLNodeSet::print(void) {
00466 int i;
00467
00468
00469 for(i = 0; i < this->getNodeNum(); i++) {
00470 CXMLNode(this->m_nodeSet->nodeTab[i]).print(true);
00471 }
00472 }
00473 };