ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: XML :: XML ½âÎöÆ÷

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

XML
XML DHTMLÐÐΪ
XML Ïà¹Ø¼¼Êõ
XML ±à¼­Æ÷
XML ÕªÒª
XML ʵÀý
XML×Ö·û±àÂë
xml ÎĵµÊ÷
IEºÍ»ðºü¶ÁÈ¡XML·..

XML ½âÎöÆ÷


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2008-04-09 ä¯ÀÀ:5923 :: ::

To read and update - create and manipulate - an XML document, you will need an XML parser.
Èç¹ûÄãÏ£ÍûÔĶÁ¡¢¸üС¢´´½¨ÒÔ¼°²Ù×÷Ò»·ÝXMLÎĵµ£¬Äã¾ÍÐèÒªÓõ½Ò»¸öXML½âÎöÆ÷¡£


Microsoft's XML Parser
΢Èí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½âÎöÆ÷µÄ¾ßÌåÌØÕ÷£º

  • JavaScript, VBScript, Perl, VB, Java, C++, etc. support
    Ö§³ÖJavaScript¡¢VBScript¡¢Perl¡¢VB¡¢Java¡¢C++µÈÓïÑÔ¡£
  • Complete XML support
    Íêȫ֧³ÖXML¡£
  • Full DOM and Namespace support
    Íêȫ֧³ÖDOM ºÍ ÃüÃû¿Õ¼ä
  • DTD and validation support
    Ö§³ÖDTD ºÍÓÐЧÐÔÑéÖ¤
  • Complete XSLT and XPath support
    Íêȫ֧³ÖXSLT ºÍ XPath
  • SAX2 support
    Ö§³ÖSAX2
  • Server-safe HTTP support
    Ö§³ÖHTTP·þÎñÆ÷°²È«Ð­Òé

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">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
...
...
...
</script>

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Îĵµ£»µÚ¶þÐйرÕÁËͬ²½¼ÓÔØ¹¦ÄÜ£¬È·±£½âÎöÆ÷ÔÚÎĵµ³¹µ×¼ÓÔØÍê±Ï֮ǰ²»»á¼ÌÐøÖ´Ðнű¾Óï¾ä¡£


XML Parser in Mozilla Browsers
Mozillaä¯ÀÀÆ÷ÉϵÄ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">

var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
...
...
...
</script>

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Îĵµ¡£


Loading an XML File - A Cross browser Example
¼ÓÔØÒ»·Ý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>
<head>
<script type="text/javascript">
var xmlDoc
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage()
}
// code for Mozilla, etc.
else if (document.implementation &&

document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getmessage
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].firstChild.nodeValue
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue
}

</script>
</head>
<body onload="loadXML()" bgcolor="yellow">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />

<b>From:</b> <span id="from"></span>
<hr />
<b>Message:</b> <span id="message"></span>

</p>
</body>
</html>

Try it yourself
×Ô¼º³¢ÊÔÒ»ÏÂ

There are more examples of this in our XML DOM tutorial.
²ì¿´¸ü¶àµÄ°¸ÀýÇå·ÃÎÊÎÒÃÇµÄ XML DOM ½Ì³Ì¡£


Loading XML Text Into the Parser
°ÑXML Îı¾ÔØÈë½âÎöÆ÷

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>"
txt=txt+"<to>Tove</to><from>Jani</from>"
txt=txt+"<heading>Reminder</heading>"
txt=txt+"<body>Don't forget me this weekend!</body>"

txt=txt+"</note>"
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(txt)
...
...
...
</script>

If you have Internet Explorer, you can try it yourself.
Èç¹ûÄãÓÐIE£¬Äã¿ÉÒÔ ×Ô¼º³¢ÊÔһϡ£

ÆÀÂÛ (0) All