C++Öд¦ÀíXMLÎļþ
дUnmanaged CodeÔÚ.NETʱ´ú³ÉΪһÖֺܱ¯²ÒµÄÊ£¬µ±ÄãÐèÒª´¦ÀíXMLÎļþʱ£¬ÕâÖָоõ»á±äµÃÓÈÆäÇ¿ÁÒ¡£FCLÖеÄSystem.XML¶à¼òµ¥°¡£¬Á¬Steve Ballmer¶¼ÖªµÀÔõôÓá£
ÊÂÇé²»»á×ÜÊÇÄÇôÀíÏëµÄ£¬¼ÙÈçÄãÒªÔÚC/C++³ÌÐòÀï´¦ÀíXMLÔõô°ìÄØ£¿
Ñ¡ÔñÒ»£ºÊÐÃæÉϵÄXML lib»¹ÊÇÓм¸¸öµÄ£¬×îÓÐÃûµÄµ±È»ÊÇlibXML¡£ÎÒÒ»ÄêÇ°Óùý£¬ºÜ²»´í£¬ÎÒ»¹ÌØÒâдÁËÒ»·Ý¼òÃ÷½Ì³Ì£¬ºóÀ´²»Öª¸éÄĶùÁË¡£
Ñ¡Ôñ¶þ£ºMSµÄMSXML£¬ÎÒÒª½éÉܵľÍÊÇÕâ¸ö¡£
ÏÈ˵һÏÂÔÚMSDNÄÄÀïÕÒÎĵµ°É£¬ÍùÏ¿´µÄʱºòÒ²ºÃÓиö²Î¿¼£ºÔÚIndexÀï´ò£ºWindows Media Services 9 Series SDK=>Programming Reference=>Programming Reference (C++)=>XML DOM Interfaces (C++)¡£Ê²Ã´£¿Windows Media£¿ºÇºÇ£¬²»´í£¬ÎÒ¾õµÃÕâ¸öguide·´¶øÊÇ×îÇå³þµÄ£¬ÄãÖ±½ÓÕÒMSXML£¬µÃµ½µÄ½á¹û£¬ÎÒ¾õµÃ»¹Ã»Õâ¸öºÃ¡£
ÔÚC³ÌÐòÀïµ÷ÓÃMSXML»ù±¾¾ÍÊÇÒ»¶ÑCOM½Ó¿Ú£¬²»¹ýÔÚVisual StudioÀï²Ù×÷ÏÈÒª×öµã¼òµ¥µÄÉèÖãº
ÔÚÄãµÄProjectÀïAdd References=>COM±êÇ©=>Microsoft XML v4.0£¬5.0ÆäʵҲÓÐÁË£¬µ«ÒòΪÊǺÍOfficeÒ»Æð·¢²¼µÄ£¬¾õµÃÓеã¹Ö£¬²»ÏëÓ㬷´ÕýҲδ±ØÓÃʲôºÜ¹ÖÒìµÄ¹¦ÄÜ£¬4.0¿ÉÒÔÁË¡£
È»ºóÔÚ¼ÓÈëÕâÁ½ÐУº
#include <msXML2.h>
#import <msXML4.dll>
Í·ÎļþºÍdll¿â¡£Ê²Ã´£¿ÔÚÄÄÀï¼Ó£¿Í·Îļþ»òÕßc/cppÎļþ°¡£¬ÄÄÀïºÏÊÊ·ÅÄĶù¡£
È»ºó¾Í¿ªÊ¼±à³ÌÁË£¬Ïȶ¨ÒåÁ½¸ö±ØÓõıäÁ¿£º
IXMLDOMDocumentPtr XMLFile = NULL;
IXMLDOMElement* XMLRoot = NULL;
ΪʲôÊDZØÓõģ¿¡¡¡¡º¹£®£®£®
µÚÒ»²½µ±È»Êdzõʼ»¯COM£º
if(FAILED(CoInitialize(NULL))) ....
½ÓÏÂÀ´³õʼ»¯XMLFile¶ÔÏó£º
if(FAILED(XMLFile.CreateInstance("MsXML2.DOMDocument.4.0"))) ...
È»ºó¾Í¿ÉÒÔ¼ÓÔØXMLÎļþÁË£º
_variant_t varXML(L"C: est.XML"); //L for unicode
VARIANT_BOOL varOut;
XMLFile->load(varXML, &varOut);
È¡µÃroot element£º
XMLFile->get_documentElement(&XMLRoot))
È¡µÃµÚÒ»¼¶element£º
IXMLDOMNodeList* XMLChildNodes = NULL;
XMLRoot->get_childNodes(&XMLChildNodes);
±éÀúËùÓеÚÒ»¼¶element:
IXMLDOMNode* currentNode = NULL;
while(!FAILED(XMLChildNodes->nextNode(¤tNode)) && currentNode != NULL)
{
//do something
}
È¡µÃµ±Ç°elementµÄÃû³Æ£º
BSTR nodeName;
currentNode->get_nodeName(&nodeName);
È¡µÃµ±Ç°elementµÄÒ»¸öattribute£¨¼ÙÉè
Ïà¹ØÎĵµ£º
1¡¢×÷ÓÃ
ΪÁËÔÚʵÏÖ¶à̬µÄʱºò²»Ôì³ÉÄÚ´æй¶,
Èç¹û»ùÀàÎö¹¹º¯ÊýÇ°²»¼Óvitual£¬ÅÉÉúÀà¶ÔÏó±»Ïú»Ùºó£¬Ö»»áµ÷ÓûùÀàµÄÎö¹¹º¯Êý£¬¶ø²»»áÈ¥µ÷ÓÃÅÉÉúÀàµÄÎö¹¹º¯Êý¡£
2¡¢¶ÔÓÚÕý³£µÄº¯Êý£¬Èç¹û»ùÀàÖÐÉùÃ÷Ϊvirtual£¬ÔòÅÉÉúÀà¿ÉÒÔ²»ÓÃÔÙдvirtual
// CPPTest.cpp : Defines the entry point for the console application.
// ......
»Ø¹Ë֮ǰµÄƪ·ù£¬CÓïÑÔµÄÖ÷Ì岿·Ö»ù±¾ÒѾ½éÉÜÍêÁË¡£Ö®ËùÒÔûÓнéÉÜC++µÄÏà¹ØÌØÐÔÊÇÒòΪÔÚ֮ǰµÄÎÄÕÂÖÐCºÍC++ÔÚÕâЩ·½Ã涼Óй²ÐÔ£¬ËùÒÔÔÚÃæÏò¶ÔÏó֮ǰ¡£ÎÒÃÇÏÈ°ÑÕâЩ¹²ÐÔ¸ø½éÉÜÍê¡£Ò²¾ÍÊÇ˵ÔÚ½éÉÜÃæÏò¶ÔÏó֮ǰ£¬ËùÓеÄÎÄÕ¶¼ÊÇCC++Öж¼ÄÜʹÓõġ£´ÓÕâµãÉÏÀ´¿´£¬ÏÖÔÚÕý¼«Á¦·Ü¶·ÓÚC++Õ½ÏßÉϵijõѧÕß»¹ÊǺÜÓÐÓô¦µÄ¡£
±¾Æª¼ÌÐ ......
/*
* XMLTool.cpp ÅäÖÃÎļþ½âÎö¶¯Ì¬¿â×ÊÔ´Îļþ
* Ö÷ÒªÊä³öº¯Êý½Ó¿Ú·Ö±ðʵÏÖ¶ÔÅäÖÃÎļþµÄ¶ÁÈ¡ºÍдÈ룬Õë¶Ô½á¹û¼¯µÄ¸´ÖÆÇå¿Õ£¬ºÍÁ½¸ö×Ö·û´®´¦Àíº¯Êý
* ¶ÁÈ¡ÅäÖÃÎļþ½Ó¿Ú £ºint ReadXMLFile(XML_Node_Vector *XmlNodeVector, string filepath);
* Ð´È ......