欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DOM解析XML

程序员文章站 2022-03-03 16:16:48
...
To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory and allows you to manipulate it using the DOM.

There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. The loadXMLDoc function is a cross browser script that will allow parsing in both Internet Explorer and Mozilla browsers.

The following JavaScript function is stored in the "loadxmldoc.js" file and loaded in the <head> of the examples:

function loadXMLDoc(dname) 

{

var xmlDoc;

// code for IE

if (window.ActiveXObject)

{

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

}

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)

{

xmlDoc=document.implementation.createDocument("","",null);

}

else

{

alert('Your browser cannot handle this script');

}

xmlDoc.async=false;

xmlDoc.load(dname);

return(xmlDoc);

}





--------------------------------------------------------------------------------

The code above gets the XML document from the script (dname), and checks to see if what kind of browser is used. When the browser type is found, it creates the right parser type:



Microsoft's XML Parser

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.

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.

The Microsoft XML parser is created with the following code:

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");



--------------------------------------------------------------------------------

XML Parser in Mozilla, Firefox, and Opera

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.

The XML parser in Mozilla browsers is created with the following code:

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.


--------------------------------------------------------------------------------

Loading the File and Returning the XML Document

After the right parser is created the following code is run:

xmlDoc.async=false;

xmlDoc.load(dname);

return(xmlDoc);

=======分割线一=======
1。获取一个元素值
<script type="text/javascript">
xmlDoc=loadXMLDoc("/text/books.xml");
var x=xmlDoc.getElementsByTagName('title');
for (i=0;i<x.length;i++)

{

document.write(x[i].childNodes[0].nodeValue)

document.write("<br />")

}

</script>

2。获取一个属性值
TAG.getAttribute("Attribute_name")