Read XML document instance code through JavaScript Parsing

Source: Internet
Author: User

JavaScript parsing reads XML files, mainly load and parse XML files, and then you can test the content of the parsed XML file and print the output.
Online Demo: http://demo.jb51.net/js/2012/readxml/
Note: during the test, you need to test on the website, iis or apache. Be sure not to run the test by double-clicking locally.
Index.htm
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> feet home </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>
</Head>
<Body onload = "show ()">
<Div id = "tb"> </div>
</Body>
</Html>

Xml file
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menus>
<Menu id = "0" name = "Homepage">
<MenuItemTitle sid = "01" mid = "0" name = "common options">
<MenuItem mid = "0" tid = "01" url = "Home. aspx"> background homepage </MenuItem>
<MenuItem mid = "0" tid = "01" url = "Test. aspx"> Test page </MenuItem>
</MenuItemTitle>
</Menu>
<Menu id = "0" name = "try">
<MenuItemTitle sid = "01" mid = "0" name = "common options">
<MenuItem mid = "0" tid = "01" url = "Home. aspx"> background homepage </MenuItem>
<MenuItem mid = "0" tid = "01" url = "Test. aspx"> Test page </MenuItem>
</MenuItemTitle>
</Menu>
</Menus>

TABLE is not used, because the display is inverted, so a variable is set and then displayed! It is worth your signature in the future!

The following is another example:
A JavaScript class is written to read data from an XML file. The implementation code is as follows:
Copy codeThe Code is as follows:
<Script>
/**
* @ Author Shirdrn
*/
Function XMLDoc () {}; // defines an XMLDoc class
XMLDoc. prototype. xmlFile = ""; // xmlFile is a member of XMLDoc. It refers to the ". xml" file.
XMLDoc. prototype. parseXMLDoc = function () {// Method for loading the Member for parsing XML files
Var docParser;
If (window. ActiveXObject) {// Explorer support
DocParser = new ActiveXObject ("Microsoft. XMLDOM ");
DocParser. async = "false ";
DocParser. load (this. xmlFile );
Return docParser;
}
Else if (window. DOMParser) {// supported by the Illia Browser
DocParser = new DOMParser ()
Return docParser. parseFromString (this. xmlFile, "text/xml ");
}
Else {// It cannot be parsed if it is not the IE or Illia browser, and false is returned.
Return false;
}
}
XMLDoc. prototype. print = function (readTagName, readTagCnt) {// print the content of the XML file read from the output
Var xmlDoc = this. parseXMLDoc (); // call the member method parseXMLDoc () to load the parsing XML file
Var users = xmlDoc. getElementsByTagName (readTagName); // an array of users that obtains the data of the specified tag name
For (var I = 0; I <users. length; I ++) {// double loop iterative output
Document. write ("<B> NO." + (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 an XMLDoc IDE object instance
XmlDoc. xmlFile = "user. xml"; // sets the data of the member variables of the object instance.
XmlDoc. print ("user", 6); // print the output
</Script>

The content of the user. XML file used for testing is as follows:
Copy codeThe Code is 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> female </gender>
<Email> linda@hotmail.com </email>
<Phone> 13843140002 </phone>
</User>
</Users>

Run the test program. The parsing result is as follows:
Copy codeThe Code is as follows:
1st record information:
The id = 22240319830000.
Name = Shirdrn.
Age = 26.
Gender = male.
Mail = shirdrn@hotmail.com.
Phone = 13843140000.
2nd record information:
The id = 22040319860001.
Name = Linda.
Age = 23.
Gender = female.
Mail = linda@hotmail.com.
Phone = 13843140002.

When parsing XML files, make sure to provide support for different types of browsers. Here, we mainly provide support for IE and Illia browsers. Otherwise, the parsing may fail.
For other instructions, see the notes 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.