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
CopyCode The 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 + "<H2>" + menu. childnodes [0]. getattribute ("name") + "</H2> <br> ";
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 code The 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>
here, we test the XML file user. the XML content is as follows: copy Code the code is as follows:
-
-
22240319830000
shirdrn
26
male
shirdrn@hotmail.com
13843140000
-
22040319860001
Linda
23
female
linda@hotmail.com
13843140002
Run the testProgram, The output of the parsing result is as follows:Copy codeCode: 1st records:
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.