// XmlCreationDemo.cpp
#include <stdlib.h>
#include <stdio.h>
// imports MSXML parser
#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 file
IXMLDOMDocumentPtr pDoc = NULL; // xml document
IXMLDOMProcessingInstructionPtr pProInstruction = NULL; // xml声明
IXMLDOMCommentPtr pComment = NULL; // comment
IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // root node (element)
IXMLDOMNodePtr pNode = NULL, pNode1 = NULL, pNode2 = NULL; // node
IXMLDOMAttributePtr pAttrNode = NULL; // Properties
HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40)); //
if (FAILED(hr))
{
printf ( "Unable to create DOMDocument40 objects, check for installed and initialized MsXml Parser Library!");
return EXIT_FAILURE;
}
// (1) create xml document declaration (or insertBefore root)
pProInstruction = pDoc->createProcessingInstruction((_bstr_t)(char*)"xml", (_bstr_t)(char*)"version=\"1.0\" encoding=\"utf-8\"");
pDoc->appendChild((IXMLDOMNode*)pProInstruction);
// (2) Create a root <China>
pRootElement = pDoc->createElement((_bstr_t)(char*)"China");
pDoc->PutRefdocumentElement(pRootElement); // pXMLDomDoc->documentElement = pRootElement;
// (3) create a node <China> <Continent>
pComment = pDoc-> createComment ((_ bstr_t) (char *) "where the continent");
pRootElement-> appendChild ((IXMLDOMNode *) pComment); // comments
pNode = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"Continent", (_bstr_t)(char*)"");
pNode->Puttext((_bstr_t)(char*)"Asia"); // pNode->text = "Asia";
pRootElement-> appendChild (pNode); // node
// (4) create a node <China> <Population>
pComment = pDoc-> createComment ((_ bstr_t) (char *) "population");
pRootElement-> appendChild ((IXMLDOMNode *) pComment); // comments
pElement = pDoc->createElement((_bstr_t)(char*)"Population");
pAttrNode = pDoc->createAttribute((_bstr_t)(char*)"Units");
pAttrNode->Puttext((_bstr_t)(char*)"Million Person");
pElement-> setAttributeNode (pAttrNode); // statistical unit
pElement-> setAttribute ((_ bstr_t) (char *) "StatisticalYear", (_variant_t) (char *) "2000"); // Statistics Year
pElement->Puttext((_bstr_t)(char*)"1,296");
pRootElement-> appendChild (pElement); // node
// (5) create a node <China> <Municipality>
pComment = pDoc-> createComment ((_ bstr_t) (char *) "four municipality");
pRootElement-> appendChild ((IXMLDOMNode *) pComment); // comments
pNode = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"Municipality", (_bstr_t)(char*)"");
pRootElement-> appendChild (pNode); // node
// (6) create a node <China> <Municipality> <TianJin>
pNode1 = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"TianJin", (_bstr_t)(char*)"");
// Create a node <China> <Municipality> <TianJin> <Area>
pElement = pDoc->createElement((_bstr_t)(char*)"Area");
pElement-> setAttribute ((_ bstr_t) (char *) "Units", (_variant_t) (char *) "Thousand Square kilometers"); // statistical unit
pElement->Puttext((_bstr_t)(char*)"12");
pNode1-> appendChild ((IXMLDOMNode *) pElement); // node
// Create a node <China> <Municipality> <TianJin> <Population>
pElement = pDoc->createElement((_bstr_t)(char*)"Population");
pElement-> setAttribute ((_ bstr_t) (char *) "Units", (_variant_t) (char *) "Million Person"); // statistical unit
pElement-> setAttribute ((_ bstr_t) (char *) "StatisticalYear", (_variant_t) (char *) "2000"); // Statistics Year
pElement->Puttext((_bstr_t)(char*)"10.01");
pNode1-> appendChild ((IXMLDOMNode *) pElement); // node
pNode->appendChild(pNode1);
// (7) create a node <China> <Municipality> <BeiJing> and insert <TianJin> before
pNode2 = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"BeiJing", (_bstr_t)(char*)"");
// Create a node <China> <Municipality> <BeiJing> <Area>
pElement = pDoc->createElement((_bstr_t)(char*)"Area");
pElement-> setAttribute ((_ bstr_t) (char *) "Units", (_variant_t) (char *) "Thousand Square kilometers"); // statistical unit
pElement->Puttext((_bstr_t)(char*)"17");
pNode2-> appendChild ((IXMLDOMNode *) pElement); // node
// Create a node <China> <Municipality> <BeiJing> <Population>
pElement = pDoc->createElement((_bstr_t)(char*)"Population");
pElement-> setAttribute ((_ bstr_t) (char *) "Units", (_variant_t) (char *) "Million Person"); // statistical unit
pElement-> setAttribute ((_ bstr_t) (char *) "StatisticalYear", (_variant_t) (char *) "2000"); // Statistics Year
pElement->Puttext((_bstr_t)(char*)"13.82");
pNode2-> appendChild ((IXMLDOMNode *) pElement); // node
pNode->insertBefore(pNode2, (_variant_t)(IDispatch*)pNode1);
//
// (8) to create a node <China> <Municipality> <ShangHai>
// (9) create a node <China> <Municipality> <ChongQing>
pDoc->save((_variant_t)szXmlFile);
return EXIT_SUCCESS;
}
=========================== generated china.xml document content: =============== =======================================
<?xml version="1.0" encoding="utf-8"?>
<China>
<! - where the continent ->
<Continent>Asia</Continent>
<! - population ->
<Population Units="Million Person" StatisticalYear="2000">1,296</Population>
<! - four municipalities ->
<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>
=================================
two. MsXml parse XML documents Example:
// XmlParsingDemo.cpp
#include <stdlib.h>
#include <stdio.h>
// imports MSXML parser
#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 document created // Part
IXMLDOMDocumentPtr pDoc = NULL; // xml document
IXMLDOMNodeListPtr pNodeList = NULL; // node list
IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // root node (element)
IXMLDOMNodePtr pNode = NULL, pNode1 = NULL; // node
IXMLDOMNamedNodeMapPtr pAttrList = NULL; // attribute list
IXMLDOMAttributePtr pAttrNode = NULL; // Properties
long lChilds, lAttr, i;
HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40));
if (FAILED(hr))
{
printf ( "Unable to create DOMDocument40 objects, check for installed and initialized MsXml Parser Library!");
return EXIT_FAILURE;
}
VARIANT_BOOL bXmlLoad = pDoc->load((_variant_t)szXmlFile);
if (! bXmlLoad) // failed to load
{
printf ( "!% s failed to load \ n", szXmlFile);
return EXIT_FAILURE;
}
// (1) root
pRootElement = pDoc->GetdocumentElement();
printf("root = %s\n", (char*)pRootElement->GetnodeName()); // pRootElement->nodeName
@ (2) a child node of the root node
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) // comment node Filter
{
printf("child[%d] of [%s]: [%s]\n", i ,(char*)pRootElement->GetnodeName(), (char*)pNode->GetnodeName());
}
}
// (3) statistical document all <Population> node
pNodeList = pDoc->getElementsByTagName((_bstr_t)(char*)"Population");
lChilds = pNodeList->Getlength();
printf ( "document [Population] There are% d \ n", lChilds);
// <Population> node under (4) root
pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Population");
When the root node is known // <China>: pNode = pDoc-> selectSingleNode ((_ bstr_t) (char *) "China // Population");
printf ( "[Population] child node below the root node of% 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());
}
All sub-node // (5) find the node <Municipality> under
// "//" indicates that at any one looking Municipality; all child nodes "// *" Find <Municipality> </ Municipality> in
pNodeList = pDoc-> selectNodes ((_ bstr_t) (char *) "// Municipality // *"); // herein may be replaced pRootElement pDoc
while (pNode = pNodeList->nextNode())
{
printf("childs of [Municipality]: %s\n", (char*)pNode->GetnodeName());
}
A // child node (6) lookup node <Municipality> under
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) query parent, child, brother, brother node
pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"//TianJin");
pNode1 = pNode-> GetparentNode (); // parent node
printf ( "[TianJin] parent to [% s] \ n", (char *) pNode1-> GetnodeName ());
pNodeList = pNode-> GetchildNodes (); // child node
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 (); // brother node
printf ( "brother node [TianJin] is [% s] \ n", (char *) pNode1-> GetnodeName ());
pNode1 = pNode-> GetnextSibling (); // brother node
printf ( "brother node [TianJin] is [% s] \ n", (char *) pNode1-> GetnodeName ());
return EXIT_SUCCESS;
}
==================================================
Results are as follows:
root = China
child[1] of <China>: <Continent>
child[3] of <China>: <Population>
child[5] of <China>: <Municipality>
文档中<Population>共有5个
根节点下的<Population>子节点值为1,296
Attr[0] of <Population>: Units = Million Person
Attr[1] of <Population>: StatisticalYear = 2000
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>
Reproduced in: https: //www.cnblogs.com/areliang/archive/2011/11/18/2254334.html