JavaScript parsing read XML document instance code _javascript tips

Source: Internet
Author: User
Tags tagname
JavaScript parsing reads an XML file, essentially loading and parsing an XML file, and then it can test the contents of the parsed XML file and print out the output.
Online Demo: http://demo.jb51.net/js/2012/readxml/
Note: Test needs to be tested in the Web site, IIS or Apache, be careful not to double-click to run the test locally
index.htm
Copy Code code as follows:

<title> Cloud-dwelling community </title>
<script type= "Text/javascript" >
Function Show ()
{
if (window. XMLHttpRequest)
{
Xmlhttp=new XMLHttpRequest ();
}
Else
{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
Xmlhttp.open ("Get", "Jb51.xml", false);
Xmlhttp.send (NULL);
var responsexml=xmlhttp.responsexml;
var menus=responsexml.getelementsbytagname ("menus") [0];
var html= "";
for (Var i=0;i<menus.childnodes.length;i++)
{
var menu=menus.childnodes[i];
html=html+ "html=html+ "for (Var j=0;j<menu.childnodes[0].childnodes.length;j++)
{
var mi=menu.childnodes[0].childnodes[j];
var url=mi.getattribute ("url");
var Txt=mi.childnodes[0].nodevalue;
html=html+ "<a href=\" "+url+" \ ">" +txt+ "</a><br>";
}
}
document.getElementById ("TB"). innerhtml=html;
}
Else
{
Alert ("What browser do you use?") ");
}
}
</script>
<body onload= "Show ()" >
<div id= "TB" ></div>
</body>

XML file
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<Menus>
<menu id= "0" name= "Home" >
<menuitemtitle sid= mid= "0" name= "common Options" >
<menuitem mid= "0" tid= "a" url= "home.aspx" > Background home </MenuItem>
<menuitem mid= "0" tid= "a" url= "test.aspx" > Test page </MenuItem>
</MenuItemTitle>
</Menu>
<menu id= "0" name= "Try" >
<menuitemtitle sid= mid= "0" name= "common Options" >
<menuitem mid= "0" tid= "a" url= "home.aspx" > Background home </MenuItem>
<menuitem mid= "0" tid= "a" url= "test.aspx" > Test page </MenuItem>
</MenuItemTitle>
</Menu>
</Menus>

The table is not used because the display is inverted, so set a variable and then display it! It's worth the next sign!

Here is another example:
A JavaScript class is written to read the data in an XML file, and the implementation code looks like this:
Copy Code code as follows:

<script>
/**
* @author SHIRDRN
*/
function xmldoc () {}; Define a XmlDoc class
XMLDoc.prototype.xmlFile = ""; XMLFile is a member of the xmldoc, which refers to the ". Xml" File
XMLDoc.prototype.parseXMLDoc = function () {//Load member method of parsing XML file
var Docparser;
if (window. ActiveXObject) {//IE browser support
Docparser = new ActiveXObject ("Microsoft.XMLDOM");
Docparser.async = "false";
Docparser.load (This.xmlfile);
return docparser;
}
else if (window. Domparser) {//Mozillia browser support
Docparser = new Domparser ()
Return docparser.parsefromstring (This.xmlfile, "text/xml");
}
else {//if not IE and mozillia browsers cannot parse, returns false.
return false;
}
}
XMLDoc.prototype.print = function (readtagname,readtagcnt) {///print output read content information for XML file
var xmldoc = This.parsexmldoc (); Invoke member Method Parsexmldoc () load parse XML file
var users = Xmldoc.getelementsbytagname (readtagname); Gets an array of data for the specified label name users
for (var i=0; i<users.length; i++) {//double loop iterative output
document.write ("<B> section" + (i+1) + "record Information:</b><br>");
for (var j=0; j<readtagcnt; j + +) {
var tagname = Users[i].childnodes[j].tagname;
var TextValue = Users[i].childnodes[j].text;
document.write (tagname + "=" + TextValue + ".<br>");
}
}
}

var xmldoc = new XmlDoc (); Create a xmldoc instance of an IDE object
Xmldoc.xmlfile = "User.xml"; Set data for a member variable of an object instance
Xmldoc.print ("User", 6); Print output
</script>

The contents of the XML file user.xml that we used to test are shown below:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
-<users>
-<user>
<id>22240319830000</id>
<name>Shirdrn</name>
<age>26</age>
<gender> male </gender>
<email>shirdrn@hotmail.com</email>
<phone>13843140000</phone>
</user>
-<user>
<id>22040319860001</id>
<name>Linda</name>
<age>23</age>
<gender> Women </gender>
<email>linda@hotmail.com</email>
<phone>13843140002</phone>
</user>
</users>

Run the test program and parse the result output as follows:
Copy Code code as follows:

1th Record Information:
id = 22240319830000.
Name = Shirdrn.
Age = 26.
gender = male.
email = shirdrn@hotmail.com.
Phone = 13843140000.
2nd Record Information:
id = 22040319860001.
Name = Linda.
Age = 23.
gender = female.
email = linda@hotmail.com.
Phone = 13843140002.

When parsing XML files, be sure to provide support for different types of browsers, mainly for IE and mozillia browsers, otherwise it may not be resolved.
For additional instructions, refer to the comments in the program.
Related Article

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.