一.MsXml建立XML文檔示例html
// XmlCreationDemo.cpp #include <stdlib.h> #include <stdio.h> // 引入MSXML解析器 #import <msxml4.dll> using namespace MSXML2; class InitializeCom { public: InitializeCom() { CoInitialize(NULL); } ~InitializeCom() { CoUninitialize(); } }InitCom; int main() { char *szXmlFile = "D://china.xml"; // xml文件 MSXML2::IXMLDOMDocumentPtr pDoc = NULL; // xml文檔 MSXML2::IXMLDOMProcessingInstructionPtr pProInstruction = NULL; // xml聲明 MSXML2::IXMLDOMCommentPtr pComment = NULL; // 註釋 MSXML2::IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根節點(元素) MSXML2::IXMLDOMNodePtr pNode = NULL, pNode1 = NULL, pNode2 = NULL; // 節點 MSXML2::IXMLDOMAttributePtr pAttrNode = NULL; // 屬性 HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40)); // if (FAILED(hr)) { printf("沒法建立DOMDocument40對象,請檢查是否安裝並初始化了MsXml Parser庫!"); return EXIT_FAILURE; } // (1)建立xml文檔聲明(或insertBefore根節點) pProInstruction = pDoc->createProcessingInstruction((_bstr_t)(char*)"xml", (_bstr_t)(char*)"version=\"1.0\" encoding=\"utf-8\""); pDoc->appendChild((MSXML2::IXMLDOMNode*)pProInstruction); // (2)建立根節點<China> pRootElement = pDoc->createElement((_bstr_t)(char*)"China"); pDoc->PutRefdocumentElement(pRootElement); // pXMLDomDoc->documentElement = pRootElement; // (3)建立節點<China><Continent> pComment = pDoc->createComment((_bstr_t)(char*)"所在的洲"); pRootElement->appendChild((MSXML2::IXMLDOMNode*)pComment); // 註釋 pNode = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"Continent", (_bstr_t)(char*)""); pNode->Puttext((_bstr_t)(char*)"Asia"); // pNode->text = "Asia"; pRootElement->appendChild(pNode); // 節點 // (4)建立節點<China><Population> pComment = pDoc->createComment((_bstr_t)(char*)"人口數量"); pRootElement->appendChild((MSXML2::IXMLDOMNode*)pComment); // 註釋 pElement = pDoc->createElement((_bstr_t)(char*)"Population"); pAttrNode = pDoc->createAttribute((_bstr_t)(char*)"Units"); pAttrNode->Puttext((_bstr_t)(char*)"Million Person"); pElement->setAttributeNode(pAttrNode); // 統計單位 pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 統計年份 pElement->Puttext((_bstr_t)(char*)"1,296"); pRootElement->appendChild(pElement); // 節點 // (5)建立節點<China><Municipality> pComment = pDoc->createComment((_bstr_t)(char*)"四個直轄市"); pRootElement->appendChild((MSXML2::IXMLDOMNode*)pComment); // 註釋 pNode = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"Municipality", (_bstr_t)(char*)""); pRootElement->appendChild(pNode); // 節點 // (6)建立節點<China><Municipality><TianJin> pNode1 = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"TianJin", (_bstr_t)(char*)""); // 建立節點<China><Municipality><TianJin><Area> pElement = pDoc->createElement((_bstr_t)(char*)"Area"); pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 統計單位 pElement->Puttext((_bstr_t)(char*)"12"); pNode1->appendChild((MSXML2::IXMLDOMNode*)pElement); // 節點 // 建立節點<China><Municipality><TianJin><Population> pElement = pDoc->createElement((_bstr_t)(char*)"Population"); pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 統計單位 pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 統計年份 pElement->Puttext((_bstr_t)(char*)"10.01"); pNode1->appendChild((MSXML2::IXMLDOMNode*)pElement); // 節點 pNode->appendChild(pNode1); // (7)建立節點<China><Municipality><BeiJing>並插入<TianJin>前 pNode2 = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"BeiJing", (_bstr_t)(char*)""); // 建立節點<China><Municipality><BeiJing><Area> pElement = pDoc->createElement((_bstr_t)(char*)"Area"); pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 統計單位 pElement->Puttext((_bstr_t)(char*)"17"); pNode2->appendChild((MSXML2::IXMLDOMNode*)pElement); // 節點 // 建立節點<China><Municipality><BeiJing><Population> pElement = pDoc->createElement((_bstr_t)(char*)"Population"); pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 統計單位 pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 統計年份 pElement->Puttext((_bstr_t)(char*)"13.82"); pNode2->appendChild((MSXML2::IXMLDOMNode*)pElement); // 節點 pNode->insertBefore(pNode2, (_variant_t)(IDispatch*)pNode1); // // (8)建立節點<China><Municipality><ShangHai> // (9)建立節點<China><Municipality><ChongQing> pDoc->save((_variant_t)szXmlFile); return EXIT_SUCCESS; }生成的china.xml文檔內容:
<?xml version="1.0" encoding="utf-8"?> <China> <!--所在的洲--> <Continent>Asia</Continent> <!--人口數量--> <Population Units="Million Person" StatisticalYear="2000">1,296</Population> <!--四個直轄市--> <Municipality> <BeiJing> <Area Units="Thousand Square kilometers">17</Area> <Population Units="Million Person" StatisticalYear="2000">13.82</Population> </BeiJing> <TianJin> <Area Units="Thousand Square kilometers">12</Area> <Population Units="Million Person" StatisticalYear="2000">10.01</Population> </TianJin> <ShangHai> <Area Units="Thousand Square kilometers">6.4</Area> <Population Units="Million Person" StatisticalYear="2000">16.74</Population> </ShangHai> <ChongQing> <Area Units="Thousand Square kilometers">84</Area> <Population Units="Million Person" StatisticalYear="2000">30.90</Population> </ChongQing> </Municipality> </China>二.MsXml解析XML文檔示例
// XmlParsingDemo.cpp #include <stdlib.h> #include <stdio.h> // 引入MSXML解析器 #import <msxml4.dll> using namespace MSXML2; class InitializeCom { public: InitializeCom() { CoInitialize(NULL); // Initializes the COM library } ~InitializeCom() { CoUninitialize(); // Closes the COM library } }InitCom; int main() { char *szXmlFile = "D://china.xml"; //上篇建立的xml文檔 IXMLDOMDocumentPtr pDoc = NULL; // xml文檔 IXMLDOMNodeListPtr pNodeList = NULL; // 節點鏈表 IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根節點(元素) IXMLDOMNodePtr pNode = NULL, pNode1 = NULL; // 節點 IXMLDOMNamedNodeMapPtr pAttrList = NULL; // 屬性鏈表 IXMLDOMAttributePtr pAttrNode = NULL; // 屬性 long lChilds, lAttr, i; HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40)); if (FAILED(hr)) { printf("沒法建立DOMDocument40對象,請檢查是否安裝並初始化了MsXml Parser庫!"); return EXIT_FAILURE; } VARIANT_BOOL bXmlLoad = pDoc->load((_variant_t)szXmlFile); if (!bXmlLoad) // 加載失敗 { printf("加載%s失敗!/n", szXmlFile); return EXIT_FAILURE; } // (1)根節點 pRootElement = pDoc->GetdocumentElement(); printf("root = %s/n", (char*)pRootElement->GetnodeName()); // pRootElement->nodeName // (2)根節點的一級子節點 pNodeList = pRootElement->GetchildNodes(); // pRootElement->childNodes lChilds = pNodeList->Getlength(); // pNodeList->length for (i = 0; i < lChilds; i++) { pNode = pNodeList->Getitem(i); // pNodeList->item[i] if (pNode->GetnodeType() != NODE_COMMENT) // 過濾註釋節點 { printf("child[%d] of [%s]: [%s]/n", i ,(char*)pRootElement->GetnodeName(), (char*)pNode->GetnodeName()); } } // (3)統計文檔中全部的<Population>節點 pNodeList = pDoc->getElementsByTagName((_bstr_t)(char*)"Population"); lChilds = pNodeList->Getlength(); printf("文檔中[Population]共有%d個/n", lChilds); // (4)根節點下的<Population>節點 pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Population"); // 已知根節點爲<China>時:pNode = pDoc->selectSingleNode((_bstr_t)(char*)"China//Population"); printf("根節點下的[Population]子節點值爲%s/n", (char*)pNode->Gettext()); pAttrList = pNode->Getattributes(); lAttr = pAttrList->Getlength(); for (i = 0; i < lAttr; i++) { pAttrNode = pAttrList->Getitem(i); printf("Attr[%d] of [%s]: %s = %s/n", i, (char*)pNode->GetnodeName(), (char*)pAttrNode->GetnodeName(), (char*)pAttrNode->Gettext()); } // (5)查找節點<Municipality>下的全部子節點 // "//"表示在任意一層尋找Municipality;"//*"查找<Municipality></Municipality>中的全部子節點 pNodeList = pDoc->selectNodes((_bstr_t)(char*)"//Municipality//*"); // 這裏可將pDoc換成pRootElement while (pNode = pNodeList->nextNode()) { printf("childs of [Municipality]: %s/n", (char*)pNode->GetnodeName()); } // (6)查找節點<Municipality>下的一級子節點 pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Municipality"); pNodeList = pNode->GetchildNodes(); lChilds = pNodeList->Getlength(); for (i = 0; i < lChilds; i++) { pNode1 = pNodeList->Getitem(i); // pNodeList->item[i] printf("child[%d] of [Municipality]: %s/n", i, (char*)pNode1->GetnodeName()); } // (7)查詢父、子、兄、弟節點 pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"//TianJin"); pNode1 = pNode->GetparentNode(); // 父節點 printf("[TianJin]的父節點爲[%s]/n", (char*)pNode1->GetnodeName()); pNodeList = pNode->GetchildNodes(); // 子節點 lChilds = pNodeList->Getlength(); for (i = 0; i < lChilds; i++) { pNode1 = pNodeList->nextNode(); printf("child[%d] of [TianJin]: %s/n", i, (char*)pNode1->GetnodeName()); } pNode1 = pNode->GetpreviousSibling(); // 兄節點 printf("[TianJin]的兄節點爲[%s]/n", (char*)pNode1->GetnodeName()); pNode1 = pNode->GetnextSibling(); // 弟節點 printf("[TianJin]的弟節點爲[%s]/n", (char*)pNode1->GetnodeName()); return EXIT_SUCCESS; }
運行結果以下:node
root = Chinaapp
child[1] of <China>: <Continent>ui
child[3] of <China>: <Population>spa
child[5] of <China>: <Municipality>code
文檔中<Population>共有5個xml
根節點下的<Population>子節點值爲1,296htm
Attr[0] of <Population>: Units = Million Person對象
Attr[1] of <Population>: StatisticalYear = 2000blog
childs of <Municipality>: BeiJing
childs of <Municipality>: Area
childs of <Municipality>: Population
childs of <Municipality>: TianJin
childs of <Municipality>: Area
childs of <Municipality>: Population
childs of <Municipality>: ShangHai
childs of <Municipality>: Area
childs of <Municipality>: Population
childs of <Municipality>: ChongQing
childs of <Municipality>: Area
childs of <Municipality>: Population
child[0] of <Municipality>: BeiJing
child[1] of <Municipality>: TianJin
child[2] of <Municipality>: ShangHai
child[3] of <Municipality>: ChongQing
<TianJin>的父節點爲<Municipality>
child[0] of <TianJin>: Area
child[1] of <TianJin>: Population
<TianJin>的兄節點爲<BeiJing>
<TianJin>的弟節點爲<ShangHai>