JavaScript traversal XML File implementation program

Source: Internet
Author: User
The code is as follows Copy Code
<script>

function Getxmldoc (xmlfile) {//Create XmlDocument object, using method: Incoming XML file name.

var xmldoc;

Determine if the browser is IE

if (window. ActiveXObject) {

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

}

To determine if the browser is not Firefox and other browsers

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

Xmldoc=document.implementation.createdocument ("", "", null);

} else {

Alert (' Your browser cannot process scripts ');

}

Xmldoc.async=false; Turn off asynchronous transmissions

Xmldoc.load (xmlfile); Load XML file

return (xmldoc);

}

var xmldoc=getxmldoc ("Cd.xml"); Add XML file, create Document Object

var cds=xmldoc.getelementsbytagname ("CD"); Get all the CD labels in the XML file

alert (Cds[0].childnodes[0].childnodes[0].nodevalue); Get the contents of the first text node in the first title tag in the first CD tag

var str= ' <table border= "1″width=" 500″align= "center" >;

Traversing the contents of an XML file and outputting it in tabular form

for (var i=0; i<cds.length; i++) {

str+= "<tr>";

for (Var j=0 j<cds[i].childnodes.length; J + +) {

if (cds[i].childnodes[j].nodetype==1) {//Judge node type, because IE and Firefox two browser get XML file is different, line will also be as a node
str+= ' <td> ' +cds[i].childnodes[j].childnodes[0].nodevalue+ ' </td> ';
}

}

str+= ' </tr> ';

}

str+= ' </table> ';

</script>

<div id= "One" style= "background: #ccc" >

</div>

<script>

document.getElementById ("one"). Innerhtml=str; Output content

</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.