w3pop.com :: ÍøÂçѧԺ :: XML :: XML ½âÎöÆ÷
To read and update - create and manipulate - an XML document, you will need an XML parser.
Èç¹ûÄãÏ£ÍûÔĶÁ¡¢¸üС¢´´½¨ÒÔ¼°²Ù×÷Ò»·ÝXMLÎĵµ£¬Äã¾ÍÐèÒªÓõ½Ò»¸öXML½âÎöÆ÷¡£
Microsoft's XML parser is a COM component that comes with Internet Explorer 5 and higher. Once you have installed Internet Explorer, the parser is available to scripts.
΢ÈíµÄXML½âÎöÆ÷ÊÇÓëIE5¼°Æä¸ü¸ß°æµÄä¯ÀÀÆ÷ÕûºÏÔÚÒ»ÆðµÄCOM×é¼þ£¬Ò»µ©Äã°²×°ÁËIEä¯ÀÀÆ÷£¬¾Í¿ÉÒÔʹÓýâÎöÆ÷½âÎö½Å±¾³ÌÐòÁË¡£
Microsoft's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML.
΢ÈíµÄXML½âÎöÆ÷¾ßÓÐËùÓеıر¸¹¦ÄÜ£¬¿ÉÒÔ´©¹ý½ÚµãÊ÷£¬·ÃÎʽڵãºÍÆäÊôÐÔÖµ£¬²åÈë»òɾ³ý½Úµã£¬²¢°Ñ½ÚµãÊ÷ת»»³ÉXMLÐÎʽ¡£
The following table lists the most commonly used node types supported by Microsoft's XML parser:
ÏÂÃæµÄ±í¸ñÁоÙÁË΢ÈíµÄXML½âÎöÆ÷ËùÖ§³ÖµÄµ±Ç°×î³£ÓõĽڵãÀàÐÍ£º
| Node Type Node ÀàÐÍ |
Example ¾ÙÀý |
|---|---|
| Processing instruction [´¦ÀíÖ¸Áî] | <?xml version="1.0"?> |
| Element [ÔªËØ] | <drink type="beer">Carlsberg</drink> |
| Attribute [ÊôÐÔ] | type="beer" |
| Text [Îı¾] | Carlsberg |
MSXML Parser 2.5 is the XML parser that is shipped with Windows 2000 and IE 5.5.
MSXML Parser 2.5 ÊÇËæWindows 2000 ºÍ IE 5.5 µÄ°²×°¶ø×Ô¶¯°²×°µÄXML½âÎöÆ÷¡£
MSXML Parser 3.0 is the XML parser that is shipped with IE 6.0 and Windows XP.
MSXML Parser 3.0 ÊÇËæIE 6.0 ºÍ Windows XPµÄ°²×°¶ø×Ô¶¯°²×°µÄXML½âÎöÆ÷¡£
The MSXML 3.0 parser features:
MSXML 3.0½âÎöÆ÷µÄ¾ßÌåÌØÕ÷£º
To create an instance of Microsoft's XML parser with JavaScript, use the following code:
Óà JavaScript ÓïÑÔ±àÒ»¸ö΢ÈíXML½âÎöÆ÷µÄÀý×Ó£¬¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂ룺
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
|
To create an instance of Microsoft's XML parser with VBScript, use the following code:
Óà VBScript ±àдһ¸ö΢ÈíXML½âÎöÆ÷µÄÀý×Ó£¬¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂ룺
set xmlDoc=CreateObject("Microsoft.XMLDOM")
|
To create an instance of Microsoft's XML parser in an ASP page (using VBScript), use the following code:
ͨ¹ýʹÓÃVBScript ÔÚASPÒ³ÃæÖд´½¨Ò»¸ö΢ÈíXML½âÎöÆ÷£¬¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂ룺
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
|
The following code loads an existing XML document ("note.xml") into Microsoft's XML parser:
ÏÂÃæµÄ´úÂë°Ñµ±Ç°µÄXMLÎĵµ("note.xml")¼ÓÔØµ½ÏÖ´æµÄ΢ÈíXML½âÎöÆ÷ÖУº
<script type="text/javascript"> |
The first line of the script above creates an instance of the Microsoft XML parser. The third line tells the parser to load an XML document called "note.xml". The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded.
ÉÏÊö½Å±¾Óï¾äµÄµÚÒ»Ðд´½¨ÁËÒ»¸ö΢ÈíXML½âÎöÆ÷µÄʵÀý£»µÚÈýÐÐָʾ½âÎöÆ÷¼ÓÔØÒ»·ÝÃûΪ"note.xml" µÄXMLÎĵµ£»µÚ¶þÐйرÕÁËͬ²½¼ÓÔØ¹¦ÄÜ£¬È·±£½âÎöÆ÷ÔÚÎĵµ³¹µ×¼ÓÔØÍê±Ï֮ǰ²»»á¼ÌÐøÖ´Ðнű¾Óï¾ä¡£
Plain XML documents are displayed in a tree-like structure in Mozilla (just like IE).
ÔÚMozillaä¯ÀÀÆ÷ÖУ¬ÆÕͨµÄXMLÎĵµ½«ÒÔÊ÷×´½á¹¹±»ÏÔʾ³öÀ´£¨×ÅÀàËÆÓÚIEä¯ÀÀÆ÷£©¡£
Mozilla also supports parsing of XML data using JavaScript. The parsed data can be displayed as HTML.
MozillaÒ²Ö§³ÖʹÓÃJavaScript ½âÎöXMLÊý¾Ý¡£±»½âÎöµÄÊý¾Ý¿ÉÒÔͨ¹ýHTMLµÄÐÎʽÏÔʾ³öÀ´¡£
To create an instance of the XML parser with JavaScript in Mozilla browsers, use the following code:
ÔÚMozillaä¯ÀÀÆ÷ÉÏÓÃJavaScript ´´½¨Ò»¸ö΢ÈíXML½âÎöÆ÷µÄÀý×Ó£¬¿ÉÒÔʹÓÃÏÂÃæµÄ´úÂ룺
var xmlDoc=document.implementation.createDocument("ns","root",null)
|
The first parameter, ns, defines the namespace used for the XML document. The second parameter, root, is the XML root element in the XML file. The third parameter, null, is always null because it is not implemented yet.
µÚÒ»¸ö²ÎÊý£ºns£¬¶¨ÒåÁËXMLÎĵµËùʹÓõÄÃüÃû¿Õ¼ä£»µÚ¶þ¸ö²ÎÊý£ºroot£¬Ö¸¶¨ÁËXMLÎļþÖеÄXML¸ùÄ¿Â¼ÔªËØ£»µÚÈý¸ö²ÎÊý£ºnull£¬Í¨³£Îª¿Õ£¬ÒòΪËü²¢²»±»Ö´ÐС£
The following code loads an existing XML document ("note.xml") into Mozillas' XML parser:
ÏÂÊö´úÂ뽫µ±Ç°µÄÒ»·ÝXMLÎĵµ("note.xml")¼ÓÔØµ½MozillaµÄXML½âÎöÆ÷ÖУº
<script type="text/javascript"> |
The first line of the script above creates an instance of the XML parser. The second line tells the parser to load an XML document called "note.xml".
Óï¾äµÚÒ»Ðд´½¨ÁËXML½âÎöÆ÷µÄÀý×Ó£¬µÚ¶þÐиæËß½âÎöÆ÷×°ÔØÒ»·ÝÃûΪ"note.xml"µÄXMLÎĵµ¡£
The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser:
ÒÔÏÂÀý×Ó½âÊÍÁËÔÚ¿çä¯ÀÀÆ÷µÄÇé¿öÏ£¬ÈçºÎ½«µ±Ç°µÄXMLÎĵµ("note.xml")¼ÓÔØµ½XML½âÎöÆ÷ÖеݸÀý£º
<html> function getmessage() <body onload="loadXML()" bgcolor="yellow"> |
There are more examples of this in our XML DOM tutorial.
²ì¿´¸ü¶àµÄ°¸ÀýÇå·ÃÎÊÎÒÃÇµÄ XML DOM ½Ì³Ì¡£
Internet Explorer supports two ways of loading XML into a document object: the load() method and the loadXML() method. The load() method loads an XML file and the loadXML() method loads a text string that contains XML code.
IEä¯ÀÀÆ÷Ö§³ÖÁ½ÖÖ·½·¨½«XML¼ÓÔØµ½Îı¾¶ÔÏóÖУº“load()” ·½·¨ºÍ“loadXML()”·½·¨¡£“load()”·½·¨ÔØÈëÒ»·ÝXMLÎļþ£¬“loadXML()”·½·¨ÔØÈ뺬ÓÐXML±àÂëµÄÎı¾×Ö·û´®¡£
The following code loads a text string into Microsoft's XML parser:
ÏÂÊö´úÂ뽫һ¶ÎÎı¾×Ö·û´®¼ÓÔØµ½Î¢ÈíµÄXML½âÎöÆ÷ÖУº
<script type="text/javascript"> var txt="<note>" var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
</script> |
If you have Internet Explorer, you can try it yourself.
Èç¹ûÄãÓÐIE£¬Äã¿ÉÒÔ ×Ô¼º³¢ÊÔһϡ£
ÆÀÂÛ (0)
All