To read and update, create and manipulate an XML document, you will need an XML parser.
你需要通过一个XML解析器来阅读、更新、创建和操作一份XML文件。
--------------------------------------------------------------------------------
Examples
实例
Parse an XML file - Crossbrowser example
XML文件解析– Crossbrowser举例
This example is a cross-browser example that loads an existing XML document ("note.xml") into the XML parser.
这是一个关于把一个现有的XML文件("note.xml")加载到XML解析器中去的cross-browser实例。
Parse an XML string - Crossbrowser example
XML 字符串解析 – Crossbrowser举例
This example is a cross-browser example on how to load and parse an XML string.
这是一个如何加载并解析XML字符串的cross-browser实例。
--------------------------------------------------------------------------------
Parsing the XML DOM
解析XML DOM
To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree.
要熟练操作XML文件,你需要一个XML解析器。此解析器会加载文件到你计算机的存储器里。一旦文件被加载,通过DOM就可以对它的数据进行。DOM是将XML文件以树状结构来看的。
There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. In this tutorial we will show you how to create cross browser scripts that will work in both Internet Explorer and Mozilla browsers.
微软的XML解析器与用于Mozilla浏览器的XML解析器有点不一样。在这份教程中我们会说明如何在IE和Mozilla中创建cross browser脚本程序。
--------------------------------------------------------------------------------
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文件。
To create an instance of Microsoft's XML parser, use the following code:
要创建一个微软XML解析器,则需要用到下面的代码:
JavaScript:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
VBScript:
set xmlDoc=CreateObject("Microsoft.XMLDOM")
ASP:
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
The following code fragment loads an existing XML document ("note.xml") into Microsoft's XML parser:
下面的代码片断将一个现有的XML文件("note.xml")加载到微软XML文件:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");xmlDoc.async="false";xmlDoc.load("note.xml");
The first line of the script above creates an instance of the XML parser. 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. The third line tells the parser to load an XML document called "note.xml".
上面第一行的脚本程序创建了一个XML解析器的实例。第二行取消了同步加载,以保证文件在完全加载之前,避免解析器继续执行脚本程序。第三行语句制定解析器加载一个名为“note.xml”的XML文件
--------------------------------------------------------------------------------
XML Parser in Mozilla, Firefox, and Opera
Mozilla, Firefox, 和Opera中的 XML解析器
Mozilla'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.
Mozilla的 XML解析器支持所有贯穿整个节点树的必要函数,使其访问节点及节点属性,插入或删除节点以及把节点树重新转换为XML文件。
To create an instance of the XML parser in Mozilla browsers, use the following code:
如要创建在Mozilla浏览器中创建一个XML解析器,就要用到以下代码:
JavaScript:
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 fragment loads an existing XML document ("note.xml") into Mozillas' XML parser:
下面的代码片断在Mozillas的XML解析器中加载了一个现存的XML文件("note.xml") 。
var xmlDoc=document.implementation.createDocument("","",null);xmlDoc.load("note.xml");
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解析器的实例。第二行说明的是加载XML文件的解析器称为"note.xml"。
--------------------------------------------------------------------------------
Parsing an XML File - A Cross browser Example
解析XML 文件- Cross browser 举例
The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser:
下面是把现有XML文件("note.xml")加载到XML解析器的cross browser的实例:
<html><head><script type="text/javascript">var xmlDoc;function loadXML(){// code for IEif (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load("note.xml"); getmessage(); }// code for Mozilla, Firefox, Opera, etc.else if (document.implementation &&document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); xmlDoc.load("note.xml"); xmlDoc.οnlοad=getmessage; }else { alert('Your browser cannot handle this script'); }}function getmessage(){document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;}</script></head><body οnlοad="loadXML()"><h1>W3Schools Internal Note</h1><p><b>To:</b> <span id="to"></span><br /><b>From:</b> <span id="from"></span><br /><b>Message:</b> <span id="message"></span></p></body></html>
Output:输出:
W3Schools Internal Note
To: Tove
From: Jani
Message: Don't forget me this weekend!
--------------------------------------------------------------------------------
Important Note
重要节点
To extract the text (Jani) from an XML element like: <from>Jani</from>, the correct syntax is:
从XML元素中截取文本(Jani),(如:<from>Jani</from>),正确语法是:
getElementsByTagName("from")[0].childNodes[0].nodeValue
IMPORTANT: getElementsByTagName returns an array of nodes. The array contains all elements with the specified name within the XML document. In this case there is only one "from" element, but you still have to specify the array index ( [0] ).
重点:getElementsByTagName会将节点以一个数组的形式输出。这个数组包括了所有的元素(这些元素都是位于XML文件内的,且它们的名称是在“getElementsByTagName”后面的括号中指定的)。在这个例子里,尽管在这个例子里,它只指明了仅含有名称“from”的元素,但是你还是需要指定数组索引(array index)“[0]”。
--------------------------------------------------------------------------------
Parsing an XML String - A Cross browser Example
解析XML字符串- Cross browser实例
The following code is a cross-browser example on how to load and parse an XML string:
下面是关于如何加载和解析XML字符串的cross-browser实例。
<html><body><script type="text/javascript">var text="<note>";text=text+"<to>Tove</to>";text=text+"<from>Jani</from>";text=text+"<heading>Reminder</heading>";text=text+"<body>Don't forget me this weekend!</body>";text=text+"</note>";// code for IEif (window.ActiveXObject) { var doc=new ActiveXObject("Microsoft.XMLDOM"); doc.async="false"; doc.loadXML(text); }// code for Mozilla, Firefox, Opera, etc.else { var parser=new DOMParser(); var doc=parser.parseFromString(text,"text/xml"); }// documentElement always represents the root nodevar x=doc.documentElement;document.write("Text of first child element: ");document.write(x.childNodes[0].childNodes[0].nodeValue);document.write("<br />");document.write("Text of second child element: ");document.write(x.childNodes[1].childNodes[0].nodeValue);</script></body></html>
Output:
输出:
Text of first child element: Tove
Text of second child element: Jani
Note: Internet Explorer uses the loadXML() method to parse an XML string, while Mozilla browsers uses the DOMParser object.
注意:IE是通过loadXML() method来解析XML字符串的,而Mozilla浏览器则是使用DOMParser对象来解析的。
你需要通过一个XML解析器来阅读、更新、创建和操作一份XML文件。
--------------------------------------------------------------------------------
Examples
实例
Parse an XML file - Crossbrowser example
XML文件解析– Crossbrowser举例
This example is a cross-browser example that loads an existing XML document ("note.xml") into the XML parser.
这是一个关于把一个现有的XML文件("note.xml")加载到XML解析器中去的cross-browser实例。
Parse an XML string - Crossbrowser example
XML 字符串解析 – Crossbrowser举例
This example is a cross-browser example on how to load and parse an XML string.
这是一个如何加载并解析XML字符串的cross-browser实例。
--------------------------------------------------------------------------------
Parsing the XML DOM
解析XML DOM
To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree.
要熟练操作XML文件,你需要一个XML解析器。此解析器会加载文件到你计算机的存储器里。一旦文件被加载,通过DOM就可以对它的数据进行。DOM是将XML文件以树状结构来看的。
There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. In this tutorial we will show you how to create cross browser scripts that will work in both Internet Explorer and Mozilla browsers.
微软的XML解析器与用于Mozilla浏览器的XML解析器有点不一样。在这份教程中我们会说明如何在IE和Mozilla中创建cross browser脚本程序。
--------------------------------------------------------------------------------
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文件。
To create an instance of Microsoft's XML parser, use the following code:
要创建一个微软XML解析器,则需要用到下面的代码:
JavaScript:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
VBScript:
set xmlDoc=CreateObject("Microsoft.XMLDOM")
ASP:
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
The following code fragment loads an existing XML document ("note.xml") into Microsoft's XML parser:
下面的代码片断将一个现有的XML文件("note.xml")加载到微软XML文件:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");xmlDoc.async="false";xmlDoc.load("note.xml");
The first line of the script above creates an instance of the XML parser. 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. The third line tells the parser to load an XML document called "note.xml".
上面第一行的脚本程序创建了一个XML解析器的实例。第二行取消了同步加载,以保证文件在完全加载之前,避免解析器继续执行脚本程序。第三行语句制定解析器加载一个名为“note.xml”的XML文件
--------------------------------------------------------------------------------
XML Parser in Mozilla, Firefox, and Opera
Mozilla, Firefox, 和Opera中的 XML解析器
Mozilla'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.
Mozilla的 XML解析器支持所有贯穿整个节点树的必要函数,使其访问节点及节点属性,插入或删除节点以及把节点树重新转换为XML文件。
To create an instance of the XML parser in Mozilla browsers, use the following code:
如要创建在Mozilla浏览器中创建一个XML解析器,就要用到以下代码:
JavaScript:
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 fragment loads an existing XML document ("note.xml") into Mozillas' XML parser:
下面的代码片断在Mozillas的XML解析器中加载了一个现存的XML文件("note.xml") 。
var xmlDoc=document.implementation.createDocument("","",null);xmlDoc.load("note.xml");
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解析器的实例。第二行说明的是加载XML文件的解析器称为"note.xml"。
--------------------------------------------------------------------------------
Parsing an XML File - A Cross browser Example
解析XML 文件- Cross browser 举例
The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser:
下面是把现有XML文件("note.xml")加载到XML解析器的cross browser的实例:
<html><head><script type="text/javascript">var xmlDoc;function loadXML(){// code for IEif (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load("note.xml"); getmessage(); }// code for Mozilla, Firefox, Opera, etc.else if (document.implementation &&document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); xmlDoc.load("note.xml"); xmlDoc.οnlοad=getmessage; }else { alert('Your browser cannot handle this script'); }}function getmessage(){document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;}</script></head><body οnlοad="loadXML()"><h1>W3Schools Internal Note</h1><p><b>To:</b> <span id="to"></span><br /><b>From:</b> <span id="from"></span><br /><b>Message:</b> <span id="message"></span></p></body></html>
Output:输出:
W3Schools Internal Note
To: Tove
From: Jani
Message: Don't forget me this weekend!
--------------------------------------------------------------------------------
Important Note
重要节点
To extract the text (Jani) from an XML element like: <from>Jani</from>, the correct syntax is:
从XML元素中截取文本(Jani),(如:<from>Jani</from>),正确语法是:
getElementsByTagName("from")[0].childNodes[0].nodeValue
IMPORTANT: getElementsByTagName returns an array of nodes. The array contains all elements with the specified name within the XML document. In this case there is only one "from" element, but you still have to specify the array index ( [0] ).
重点:getElementsByTagName会将节点以一个数组的形式输出。这个数组包括了所有的元素(这些元素都是位于XML文件内的,且它们的名称是在“getElementsByTagName”后面的括号中指定的)。在这个例子里,尽管在这个例子里,它只指明了仅含有名称“from”的元素,但是你还是需要指定数组索引(array index)“[0]”。
--------------------------------------------------------------------------------
Parsing an XML String - A Cross browser Example
解析XML字符串- Cross browser实例
The following code is a cross-browser example on how to load and parse an XML string:
下面是关于如何加载和解析XML字符串的cross-browser实例。
<html><body><script type="text/javascript">var text="<note>";text=text+"<to>Tove</to>";text=text+"<from>Jani</from>";text=text+"<heading>Reminder</heading>";text=text+"<body>Don't forget me this weekend!</body>";text=text+"</note>";// code for IEif (window.ActiveXObject) { var doc=new ActiveXObject("Microsoft.XMLDOM"); doc.async="false"; doc.loadXML(text); }// code for Mozilla, Firefox, Opera, etc.else { var parser=new DOMParser(); var doc=parser.parseFromString(text,"text/xml"); }// documentElement always represents the root nodevar x=doc.documentElement;document.write("Text of first child element: ");document.write(x.childNodes[0].childNodes[0].nodeValue);document.write("<br />");document.write("Text of second child element: ");document.write(x.childNodes[1].childNodes[0].nodeValue);</script></body></html>
Output:
输出:
Text of first child element: Tove
Text of second child element: Jani
Note: Internet Explorer uses the loadXML() method to parse an XML string, while Mozilla browsers uses the DOMParser object.
注意:IE是通过loadXML() method来解析XML字符串的,而Mozilla浏览器则是使用DOMParser对象来解析的。