tinyxml用法学习

通过在http://www.grinninglizard.com/tinyxml/ 下载tinyxml最新的release版,解压后可以在docs下我们可以查看tinyxml的节本用法,为了快速的掌握,可以在class Hierarchy 中打开TiXmlBase可以发现其类图结构。

下面是我个人为了掌握或者说测试tinyxml,写的代码:

 

//#define TIXML_USE_STL #include "tinystr.h" #include "tinyxml.h" #include <string> //test #define TestRead //#define TestWrite void main() { #ifdef TestRead char *buf = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap=/"http://www.w3.org/2003/05/soap-envelope/"" " xmlns:d=/"http://schemas.xmlsoap.org/ws/2005/04/discovery/" xmlns:wsadis=/"http://schemas.xmlsoap.org/ws/2004/08/addressing/"" " xmlns:dn=/"http://www.onvif.org/ver10/network/wsdl/">" "<soap:Header><wsadis:MessageID>uuid:" "</wsadis:MessageID><wsadis:To InstanceId = /"1234/">urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsadis:To>" "<wsadis:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsadis:Action>" "</soap:Header><soap:Body><d:Probe> <d:Types> <d:shawn> <d:yogirl /> </d:shawn> </d:Types> <d:Scopes /></d:Probe></soap:Body></soap:Envelope>"; TiXmlDocument *root = new TiXmlDocument(); //root->LoadFile("test.xml"); root->Parse((const char *)buf); if (root == NULL) { printf("load file failed./n"); exit(EXIT_FAILURE); } if (root->Type() == TiXmlNode::TINYXML_DOCUMENT) { printf("i am a document!/n"); } TiXmlHandle *pHead = new TiXmlHandle(root); //TiXmlNode *pChild = root->FirstChild(); //TiXmlElement *pChild = pHead->FirstChild("soap:Envelope").FirstChild().FirstChild().Element(); TiXmlElement *rootEle = root->RootElement(); if (rootEle) { pHead->FindChild(rootEle,"d:shawn"); TiXmlElement *pChild = TiXmlHandle(pHead->m_pLookUpRoot).FirstChild().ToElement(); if (pChild == NULL) { printf("first child is NULL./n"); } printf("%s/n",pChild->Value()); } //std::string wsa_to = "wsadis:To"; // TiXmlString wsa_to("wsadis:To"); // // for (;pChild; pChild = pChild->NextSiblingElement()) // { // int type = pChild->Type(); // switch (type) // { // case TiXmlNode::TINYXML_ELEMENT: // if (wsa_to == pChild->ValueTStr()) // { // printf("%s/n",pChild->Attribute("InstanceId")); // } // break; // default: // break; // } // } if (pHead != NULL) { delete pHead; pHead = NULL; } if (root != NULL) { delete root; root = NULL; } #endif #ifdef TestWrite TiXmlDocument *doc = new TiXmlDocument(); TiXmlDeclaration *pdecl = new TiXmlDeclaration("1.0","utf-8",""); TiXmlElement *pEnvelope = new TiXmlElement("soap:Envelope"); pEnvelope->SetAttribute("xmlns:soap","http://www.w3.org/2003/05/soap-envelope"); pEnvelope->SetAttribute("xmlns:d","http://schemas.xmlsoap.org/ws/2005/04/discovery"); pEnvelope->SetAttribute("xmlns:wsadis","http://schemas.xmlsoap.org/ws/2004/08/addressing"); pEnvelope->SetAttribute("xmlns:dn","http://www.onvif.org/ver10/network/wsdl"); //header fabricate TiXmlElement *pHeader = new TiXmlElement("soap:Header"); TiXmlElement *pMessageId = new TiXmlElement("wsadis:MessageID"); TiXmlText *uuid = new TiXmlText("uuid:492febc3-6e8f-49fa-ad5b-e7d5ae402091"); pMessageId->LinkEndChild(uuid); TiXmlElement *pTo = new TiXmlElement("wsadis:To"); TiXmlText *to = new TiXmlText("urn:schemas-xmlsoap-org:ws:2005:04:discovery"); pTo->LinkEndChild(to); TiXmlElement *pAction = new TiXmlElement("wsadis:Action"); TiXmlText *action = new TiXmlText("http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"); pAction->LinkEndChild(action); pHeader->LinkEndChild(pMessageId); pHeader->LinkEndChild(pTo); pHeader->LinkEndChild(pAction); //body fabricate TiXmlElement *pBody = new TiXmlElement("soap:Body"); TiXmlElement *pProbe = new TiXmlElement("d:probe"); pBody->LinkEndChild(pProbe); pEnvelope->LinkEndChild(pHeader); pEnvelope->LinkEndChild(pBody); doc->LinkEndChild(pdecl); doc->LinkEndChild(pEnvelope); doc->SaveFile("send.xml"); TiXmlPrinter *printer = new TiXmlPrinter(); //printer->SetIndent("/t"); doc->Accept(printer); printf("%s",printer->CStr()); //printf("%s/n",rootElement->Value()); if (doc != NULL) { delete doc; doc = NULL; } #endif getchar(); } 

其中的FindChild()方法是个人实现的,不过坦白说效率着实不高,首先

xml文件组成的树的深度未知,结点数也是未知,所以我认为要查找一个

结点只能是通过遍历来解决了。

//added by shawn. TiXmlHandle TiXmlHandle::FindChild(TiXmlElement *_node, const char *value) { if (_node == 0) { return TiXmlHandle(0); } else if (strcmp(_node->Value(),value) == 0) { /*this is the node we want to find*/ m_pLookUpRoot = _node; return TiXmlHandle(_node); } else { printf("%s/n",_node->Value()); TiXmlNode *cur = _node->FirstChild(); if (cur) { TiXmlElement *next = cur->ToElement(); for (;next;next = next->NextSiblingElement()) { FindChild(next,value); } } return TiXmlHandle(0); } }

m_pLookUpRoot就是要查找的结点了~~~

你可能感兴趣的:(null,delete,SOAP,action,hierarchy,Types)