MsXml创建和解析XML示例



//解析xml XmlParsingDemo.cpp #include #include #include #include // 引入MSXML解析器 #import using namespace MSXML2; class InitializeCom { public: InitializeCom() { CoInitialize(NULL); }// Initializes the COM library } ~InitializeCom() { CoUninitialize(); }// Closes the COM library } }InitCom; enum MytagDOMNodeType { NODE_INVALID0 = 0, NODE_ELEMENT1 = 1, NODE_ATTRIBUTE2 = 2, NODE_TEXT3 = 3, NODE_CDATA_SECTION4 = 4, NODE_ENTITY_REFERENCE5 = 5, NODE_ENTITY6 = 6, NODE_PROCESSING_INSTRUCTION7 = 7, NODE_COMMENT8 = 8, NODE_DOCUMENT9 = 9, NODE_DOCUMENT_TYPE10 = 10, NODE_DOCUMENT_FRAGMENT11 = 11, NODE_NOTATION12 = 12 }; int main() { char *szXmlFile = "E://china.xml"; //上篇创建的xml文档 MSXML2::IXMLDOMDocumentPtr pDoc = NULL; // xml文档 MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; // 节点链表 MSXML2::IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素) MSXML2::IXMLDOMNodePtr pNode = NULL, pNode1 = NULL; // 节点 MSXML2::IXMLDOMNamedNodeMapPtr pAttrList = NULL; // 属性链表 MSXML2::IXMLDOMAttributePtr pAttrNode = NULL; // 属性 long lChilds, lAttr, i; HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30)); 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_COMMENT8) // 过滤注释节点 { printf("child[%d] of [%s]: [%s]\n", i ,(char*)pRootElement->GetnodeName(), (char*)pNode->GetnodeName()); } } // (3)统计文档中所有的节点 pNodeList = pDoc->getElementsByTagName((_bstr_t)(char*)"Population"); lChilds = pNodeList->Getlength(); printf("文档中[Population]共有%d个\n", lChilds); // (4)根节点下的节点 pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Population"); // 已知根节点为时: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;"//*"查找中的所有子节点 pNodeList = pDoc->selectNodes((_bstr_t)(char*)"//Municipality//*"); // 这里可将pDoc换成pRootElement while (pNode = pNodeList->nextNode()) { printf("childs of [Municipality]: %s\n", (char*)pNode->GetnodeName()); } // (6)查找节点下的一级子节点 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; }
// 创建xml XmlCreationDemo.cpp

#include 
#include 

// 引入MSXML解析器
#import 
using namespace MSXML2;

class InitializeCom
{
public:
	InitializeCom()	{	CoInitialize(NULL);	}
	~InitializeCom() {	CoUninitialize(); 	}
}InitCom;

int main()
{
	char *szXmlFile = "E://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::DOMDocument30)); //
	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)创建根节点
	pRootElement =  pDoc->createElement((_bstr_t)(char*)"China");	
	pDoc->PutRefdocumentElement(pRootElement); // pXMLDomDoc->documentElement = pRootElement;

	// (3)创建节点	
	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)创建节点
	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)创建节点
	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)创建节点
	pNode1 = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"TianJin", (_bstr_t)(char*)"");

	//    创建节点
	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); // 节点

	//    创建节点
	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)创建节点并插入前
	pNode2 = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"BeiJing", (_bstr_t)(char*)"");

	//    创建节点
	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); // 节点

	//    创建节点
	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)创建节点
	// (9)创建节点

	pDoc->save((_variant_t)szXmlFile);

	return EXIT_SUCCESS;
}
COM