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£¨¼ÙÉè
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
XML CDATA Îı¾
ËùÓÐ XML ÎĵµÖеÄÎı¾¾ù»á±»½âÎöÆ÷½âÎö¡£
Ö»ÓÐ CDATA Çø¶Î£¨CDATA section£©ÖеÄÎı¾»á±»½âÎöÆ÷ºöÂÔ¡£
Parsed Data
XML ½âÎöÆ÷ͨ³£»á½âÎö XML ÎĵµÖÐËùÓеÄÎı¾¡£
µ±Ä³¸ö XML ÔªËر»½âÎöʱ£¬Æä±êÇ©Ö®¼äµÄÎı¾Ò²»á±»½âÎö£º
<message>´ËÎı¾Ò²»á±»½âÎö</message>
½âÎöÆ÷Ö®ËùÒÔÕâô×öÊÇÒòÎ ......
1¡¢×÷ÓÃ
ΪÁËÔÚʵÏÖ¶à̬µÄʱºò²»Ôì³ÉÄÚ´æй¶,
Èç¹û»ùÀàÎö¹¹º¯ÊýÇ°²»¼Óvitual£¬ÅÉÉúÀà¶ÔÏó±»Ïú»Ùºó£¬Ö»»áµ÷ÓûùÀàµÄÎö¹¹º¯Êý£¬¶ø²»»áÈ¥µ÷ÓÃÅÉÉúÀàµÄÎö¹¹º¯Êý¡£
2¡¢¶ÔÓÚÕý³£µÄº¯Êý£¬Èç¹û»ùÀàÖÐÉùÃ÷Ϊvirtual£¬ÔòÅÉÉúÀà¿ÉÒÔ²»ÓÃÔÙдvirtual
// CPPTest.cpp : Defines the entry point for the console application.
// ......
»Ø¹Ë֮ǰµÄƪ·ù£¬CÓïÑÔµÄÖ÷Ì岿·Ö»ù±¾ÒѾ½éÉÜÍêÁË¡£Ö®ËùÒÔûÓнéÉÜC++µÄÏà¹ØÌØÐÔÊÇÒòΪÔÚ֮ǰµÄÎÄÕÂÖÐCºÍC++ÔÚÕâЩ·½Ã涼Óй²ÐÔ£¬ËùÒÔÔÚÃæÏò¶ÔÏó֮ǰ¡£ÎÒÃÇÏÈ°ÑÕâЩ¹²ÐÔ¸ø½éÉÜÍê¡£Ò²¾ÍÊÇ˵ÔÚ½éÉÜÃæÏò¶ÔÏó֮ǰ£¬ËùÓеÄÎÄÕ¶¼ÊÇCC++Öж¼ÄÜʹÓõġ£´ÓÕâµãÉÏÀ´¿´£¬ÏÖÔÚÕý¼«Á¦·Ü¶·ÓÚC++Õ½ÏßÉϵijõѧÕß»¹ÊǺÜÓÐÓô¦µÄ¡£
±¾Æª¼ÌÐ ......
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
private XmlDo ......