程序是win32控制台程序
// msxml.cpp : 定義控制台應用程序的入口點。 #include "stdafx.h" #include <iostream> #include <atlstr.h> #import <MSXML6.dll> using namespace MSXML2; using namespace std; void msxml2_init() { ::CoInitialize(NULL); } void msxml2_del() { ::CoUninitialize(); } /* nodename: 要查找的節點的名字 * xmbuf: xml字符串 * 函數功能:查找目標節點的所有屬性值 */ void find_node_attr(char *nodename, char *xmlbuf) { MSXML2::IXMLDOMDocumentPtr pDoc; HRESULT hr; hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60)); if(FAILED(hr)) { printf("無法創建DOMDocument對象,請檢查是否安裝了MS XML Parser 運行庫!\n"); return ; } /* 加載xml字符串 */ pDoc->loadXML(xmlbuf); MSXML2::IXMLDOMNodePtr pRoot_node, pNode; MSXML2::IXMLDOMNamedNodeMapPtr pAttrs; MSXML2::IXMLDOMNodePtr pAttrItem; long nCount; /* 在樹中查找名為Book的節點,"//"表示在任意一層查找 */ char tmpnodename[20] = "//"; strncat(tmpnodename, nodename, 3); pNode = pDoc->selectSingleNode(tmpnodename); /* get_attributes()來獲取該節點的所有屬性列表 */ pNode->get_attributes(&pAttrs); pAttrs->get_length(&nCount); cout << "共有" << nCount << "個屬性值" << nCount << endl; for(int i = 0 ; i < nCount ; i++) { pAttrs->get_item(i, &pAttrItem); /* nodeName, nodeTypedValue得到屬性名和屬性值 */ cout << (_bstr_t)pAttrItem->nodeName << ":" ; cout << (_bstr_t)pAttrItem->nodeTypedValue << endl; } //使用過了的指針要釋放。未使用的指針不要釋放,否則會觸發中斷錯誤。 pDoc.Release(); pNode.Release(); pAttrs.Release(); pAttrItem.Release(); } /* * 函數功能:遍歷整個xml_buf的內容並打印節點名字和節點的屬性值 */ void print_xmlnode(char *xmlbuf) { MSXML2::IXMLDOMDocumentPtr pDoc; HRESULT hr; hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60)); if(FAILED(hr)) { printf("無法創建DOMDocument對象,請檢查是否安裝了MS XML Parser 運行庫!\n"); return ; } /* 加載xml字符串 */ pDoc->loadXML(xmlbuf); MSXML2::IXMLDOMNodePtr pGwsIp_node, pRoot_node, pNode; MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL; MSXML2::IXMLDOMNodePtr pAttrItem; /* 遍歷整個xml_buf的內容並打印節點名字和節點的屬性值 */ pRoot_node = pDoc->firstChild; pGwsIp_node = pRoot_node->firstChild;//pDoc->firstChild->firstChild; MSXML2::DOMNodeType nodeType; long nCount ; //節點名稱 root下子節點的個數 unsigned int num = pRoot_node->childNodes->length; cout << "num = " << num << endl; char node_name[20] = {}; //Gettext()獲取節點值 for(int i = 0; i < num; i++) { strcpy(node_name, pRoot_node->childNodes->item[i]->Gettext()); cout << "node_name = " << node_name << endl; } /*IXMLDOMNamedNodeMap是xml中描述一個結點屬性的接口, 通過它用戶可以對某個節點的屬性進行操作*/ pGwsIp_node->get_attributes(&pAttrs); pAttrs->get_length(&nCount); for(int i = 0 ; i < nCount ; i++) { pAttrs->get_item(i,&pAttrItem); /*get_nodeName,get_nodeTypedValue得到屬性名和屬性值*/ cout << (_bstr_t)pAttrItem->nodeName << ":" ; cout << (_bstr_t)pAttrItem->nodeTypedValue << endl; } //使用過了的指針要釋放。未使用的指針不要釋放,否則會觸發中斷錯誤。 pDoc.Release(); pRoot_node.Release(); pGwsIp_node.Release(); pAttrs.Release(); pAttrItem.Release(); } int _tmain(int argc, _TCHAR* argv[]) { msxml2_init(); char buf[300] = "<gwsctrl><gws ip=\"192.168.1.24\" emac=\"D4:CA:6D:35:0D:95\" wmac=\"D4:CA:6D:11:59:9F\"/><A>一個節點</A><B>二個節點</B></gwsctrl>"; find_node_attr("gws", buf); print_xmlnode(buf); getchar(); msxml2_del(); return 0; }
運行后的效果

