¶ÔÓÚXMLÎĵµ£¬²åÈë¡¢Ð޸ġ¢¼ìË÷µÈÊý¾Ý²Ù×÷¹¤×÷¿ÉÒÔͨ¹ýÒ»¸öXML½âÎöÆ÷¼°ÆäÁоٵÄһϵÁжÔÏóÀ´Íê³É¡£ MicrosoftµÄXML½âÎöÆ÷פÁôÔÚÃûΪmsxml.dllµÄ¶¯Ì¬Á´½Ó¿âÎļþÖС£ÒòΪmsxmlÊÇÒ»¸öCOM¶ÔÏó£¬ËùÒÔ¿ÉÒÔÔÚÈκμæÈÝActiveXµÄ»·¾³ÖÐʹÓÃËü£¬ÈçÔÚVBºÍ ASPÖС£±¾ÎĽéÉÜÔÚVisual BasicÖвÙ×ÝXMLÎĵµµÄ¾ßÌå·½·¨¡£ ×°ÔØXMLÎĵµ н¨Ò»¸öVB Standard EXE¹¤³ÌÏîÄ¿¡£ÔÚForm1ÖÐÌí¼ÓÒ»¸öCommandButton¿Ø¼þ£¬½«ÆäNameºÍCaptionÊôÐÔ·Ö±ðÉèΪcmdLoadºÍLoad XML¡£ÎªÁËʹÓÃmsxml½âÎöÆ÷£¬±ØÐëÊÂÏÈÉèÖù¤³ÌÏîÄ¿¶Ômsxml.dllµÄÒýÓá£Ñ¡ÔñProject²Ëµ¥µÄ ReferencesÃüÁѡÖÐMicrosoft XML, version 2.0»ò¸ü¸ß°æ±¾¡£½«ÏÂÁдúÂë¼ÓÈë cmdLoad_Clickʼþ£º Option Explicit Dim xml As DOMDocument Private Sub cmdLoad_Click() Set xml = New DOMDocument Call xml.Load(App.Path £¦ ¡°flight.xml") ' flight.xmlΪÃèÊöº½°àÐÅÏ¢µÄXMLÎĵµ End Sub ÔËÐиóÌÐò£¬µã»÷Load XML°´Å¥¼´¿É×°ÔØXMLÎĵµ¡£ÔØÈëµÄXMLÎĵµ°üº¬Ò»¸ö°´ÕÕÊ÷×´½á¹¹ÅÅÁеĽڵã¶ÔÏ󼯺ϣ¬¶¥²ã½Úµã»ò¸ù½ÚµãÊÇ documentElement¡£Ã¿¸ö½Úµã¶¼ÓÐÒ»×éÊôÐԺͷ½·¨£¬Ã¿¸ö½Úµã¿ÉÒÔ°üº¬Ò»¸ö»ò¶à¸ö×ӽڵ㣬µ±È»Ò²¿ÉÒÔ²»°üº¬Èκνڵ㡣±ØÐëÏÈÈ¡µÃ¶Ô¸ù½ÚµãµÄÒýÓòÅÄܲÙ×ÝXMLÎĵµ¡£ÔÚ cmdLoad_Clickʼþ´úÂëºóÌí¼ÓÏÂÁдúÂ룺 Dim root As IXMLDOMElement Set root = xml.documentElement ±éÀú½Úµã¼¯ºÏ ¿ÉÒÔʹÓÃVisual BasicµÄFor Each...NextÑ»·½á¹¹ÔÚ½Úµã¶ÔÏ󼯺ÏÖÐÑ»·¡£ÔÚcmdLoad_Clickʼþ´úÂëºóÌí¼ÓÏÂÁдúÂ룺 Dim node As IXMLDOMNode For Each node In root.childNodes Debug.Print node.Text Next ÔËÐкóµã»÷Load XML°´Å¥£¬½«»áÔÚImmediate´°¿ÚÖп´µ½ÈçÏÂÊä³ö½á¹û£º 2000Äê10ÔÂ15ÈÕ ±±¾© Ê׶¼¹ú¼Ê Ïã¸Û ³àåå½Ç Öйú¹ú¼Êº½¿Õ¹«Ë¾ ²¨Òô¹«Ë¾ B747 13:00 15:55 ÉϺ£ ÆÖ¶« ·¨À¼¿Ë¸£ ÃÀÒð Ëþʲ¸É ÓÈÈÕÄÚ ººÉ¯º½¿Õ¹«Ë¾ ¿ÕÖпͳµ¹¤Òµ¹«Ë¾ A330 21:45 01:45 ×¢Ò⣬ÿ¸ö½ÚµãµÄTextÊôÐÔ²»½ö°üº¬×ÔÉíµÄÊôÐÔÖµ£¬»¹°üº¬ÆäÏÂÊôËùÓÐ×Ó½ÚµãµÄTextÊôÐÔÖµ¡£Ò»°ãÇé¿öÏ£¬²»»áͬʱ²Ù×÷ËùÓеĽڵ㡣¿ÉÒÔʹÓà selectNodes·½·¨È¡µÃ¶ÔÒ»¸öÌض¨µÄ½Úµã×Ó¼¯µÄÒýÓã¬selectNodes·½·¨·µ»ØÒ»¸ö IXMLDOMNodeList¶ÔÏó¡£ÔÚcmdLoad_Clickʼþ´úÂëºóÌí¼ÓÏÂÁдúÂ룺 Dim flightNodes As IXMLDOMNodeList Set flightNodes = root.selectNodes(¡°º½°à") For Each node In flightNodes Debug.Print node.selectSingleNode (¡°Àëվʱ¼ä").Text £¦ ¡°¡ª¡ª" £¦node.selectSingleNode (¡°µ½Õ¾Ê±¼ä").Text Next Õâ¶Î´úÂëʹÓÃselectNodes·½·¨Ñ¡Ôñ¸ù½ÚµãÏÂËùÓеġ°º½°à¡±½Úµã£¬È»ºóʹÓÃselectSingleNode·½·¨Ñ¡Ôñ¡°º½°à¡±½Úµãϵġ°Àëվʱ¼ä¡±ºÍ¡°µ½Õ¾Ê±¼ä¡±£¬¶ÁÈ¡²¢´òÓ¡ËüÃǵÄTextÊôÐÔÖµ¡£ ¿ÉÒÔʹÓÃchildNodes¼¯ºÏÒԵݹéµÄ·½Ê½²ã´Î»¯ÏÔʾXML ÎĵµµÄÄÚÈÝ¡£ Ê×ÏÈ£¬ÔÚForm1ÖÐÉùÃ÷Ò»¸ö×Ó¹ý³ÌPrettyPrint¡£ Public Sub PrettyPrint(node As IXMLDOMNode, Optional tabLevel As Integer=0) Dim childNode As IXMLDOMNode If node.nodeName<>vbNullStrin And node.nodeName < >¡°££text" Then Debug.PrintString(tabLevel,Chr¡ç(9)) £¦¡°< "£¦node.nodeName£¦¡° >" End If If node.hasChildNodes Then For Each childNode In node.childNodes Call PrettyPrint(childNode,tabLevel£«1) Next Else Debug.PrintString(tabLevel£«1,Chr¡ç(9))£¦node.Text End If If node.nodeName< >vbNullString And node.nodeName < >¡°££text" Then Debug.PrintString (tabLevel,Chr¡ç(9))£¦¡°< /"£¦node.nodeName£¦¡° >" End If End Sub È»ºóÔÚForm1ÖмÓÈëµÚ2¸öCommandButton¿Ø¼þ£¬½«Æä NameºÍCaptionÊôÐÔ·Ö±ðÉèΪcmdPrintºÍPrint¡£½«ÏÂÁдúÂë¼ÓÈëʼþcmdPrint_ClickÖУº Private Sub cmdPrint_Click() If Not xml Is Nothing Then Call PrettyPrint (xml.documentElement) End If End Sub ÔËÐгÌÐò¡£Ê×Ïȵã»÷Load XML°´Å¥¼ÓÔØflight.xml£¬È»ºóµã»÷Print°´Å¥¡£ÔÚImmediate´°¿ÚÖпÉÒÔ¿´µ½²ã´Î·ÖÃ÷¡¢Ò×ÓÚÔĶÁµÄÊä³ö½á¹û¡£×Ó¹ý³Ì PrettyPrintÔÚdocumentElementµÄ×Ó½ÚµãÖÐÑ»·£¬´òӡÿ¸ö½ÚµãµÄnodeNameÊôÐÔÖµ£¬¶øÿ¸ö×Ó½ÚµãÓֵݹéµ÷ÓÃPrettyPrint¡£XMLÎĵµÄÚ¸÷½ÚµãÄÚÔÚµÄÏàËÆÐÔÊÇXML×î¾ßÎüÒýÁ¦µÄÌØÐÔ£¬Ã¿¸ö×ӽڵ㶼ÓëÆ丸½ÚµãÓÐ×ÅÏàͬµÄÊôÐԺͷ½·¨£¨¾¡¹Ü¸÷×ÔµÄÄÚÈÝͨ³£²»Í¬£©£¬Õâ¾ÍʹµÃΪͬһÎĵµÖеÄËùÓлò²¿·Ö½Úµã±àдijÖÖ¹æÔò±äµÃÏà¶Ô¼òµ¥¡£ Ð޸ĽڵãÄÚÈÝ ÈçͬÐÞ¸ÄÊý¾Ý¿âÖеļǼһÑù£¬Ò²¿ÉÒÔÐÞ¸ÄXMLÎĵµÖнڵãµÄÄÚÈÝ¡£ÏÂÁдúÂëÐÞ¸ÄÁËflight.xmlÎĵµÖС°·¢²¼ÈÕÆÚ¡±½ÚµãµÄTextÊôÐÔÖµ£º Set node = root.selectSingleNode(¡°·¢²¼ÈÕÆÚ") If Not node Is Nothing Then node.Text = ¡°2000Äê10ÔÂ20ÈÕ" End If ÐÞ¸ÄÁËÎĵµÄÚÈÝÖ®ºó£¬¿ÉÒÔʹÓÃSave·½·¨±£´æÐ޸ĺóµÄÎĵµ¡£Save·½·¨¿ÉÒÔ½«XMLÎĵµ±£´æΪÎļþ¡¢Êý¾ÝÁ÷»òASPµÄResponse¶ÔÏó¡£Save·½·¨µÄÓ÷¨ÈçÏ£º xml.save App.Path £¦ ¡°flightupdated.xml" ½«ÉÏÃæÁ½¶Î´úÂëÌí¼ÓÔÚcmdLoad_ClickʼþÖУ¬ÔËÐгÌÐò¡£µã»÷Load XML°´Å¥£¬ÔÚ³ÌÐò¹¤×÷Ŀ¼ÖлáÔö¼ÓÒ»¸öÐÂÎļþflightupdated.xml¡£ÔÚIE5 Öдò¿ª¸ÃÎļþ£¬¿ÉÒÔ¿´µ½¡°·¢²¼ÈÕÆÚ¡±±ê¼ÇµÄÎı¾ÒѾ±»¸ÄΪ¡°2000Äê10ÔÂ20ÈÕ¡±¡£ ÔÚʵ¼ÊÓ¦ÓÃÖУ¬XML¿ÉÒÔ±»ÓÃ×÷Êý¾Ý´«Êä¸ñʽ¡¢¿ÉËÑË÷Êý¾Ý¿â¡¢¿É¶¨ÖƵÄÊý¾Ý´æ´¢·½Ê½ºÍÏÔʾÐÅÏ¢µÄÊý¾ÝÔ´£¨ÐèÒªXSL»òÈçVBÕâÑùµÄ±à³ÌÓïÑԵİïÖú£©¡£ÒòΪmsxmlÊÇÒ»¸öCOM¶ÔÏó£¬ËùÒÔ¿ÉÒÔÔÚASPÒ³ÖÐʹÓÃËüµÄʵÀý£¬È»ºó·½±ãµØʹÓÃVBScript»òJScript±à³ÌÀ´²Ù×ÝËü¡£ |