JS reads XML file data and displays data in table (ie Firefox compatible)

Source: Internet
Author: User
Tags dname

Look at the XML file first:

<?XML version= "1.0" standalone= "yes"?><Student>  <Stuinfo>    <Stuname>Zhang Qiuli</Stuname>    <Stusex>Woman</Stusex>    <Stuage>18</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Force Mogaoyizhang</Stuname>    <Stusex>Man</Stusex>    <Stuage>31</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Lis Wen</Stuname>    <Stusex>Man</Stusex>    <Stuage>22</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Ma</Stuname>    <Stusex>Woman</Stusex>    <Stuage>25</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Sun Honglei</Stuname>    <Stusex>Man</Stusex>    <Stuage>32</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Ouyang Junshong</Stuname>    <Stusex>Man</Stusex>    <Stuage>28</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Veronica</Stuname>    <Stusex>Woman</Stusex>    <Stuage>23</Stuage>  </Stuinfo>  <Stuinfo>    <Stuname>Little</Stuname>    <Stusex>Woman</Stusex>    <Stuage>22</Stuage>  </Stuinfo></Student>
View Code

ASPX page code:

<%@ Page language="C #"autoeventwireup="true"Codebehind="Get Database data generation XML.aspx.cs"inherits="Chapter1. Getting database data Generation XML"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title> <script type="Text/javascript">function Loadxmldoc (dname) {if(window. XMLHttpRequest) {xhttp=NewXMLHttpRequest (); }            Else{xhttp=NewActiveXObject ("Microsoft.XMLHTTP"); } xhttp.open ("GET", Dname,false); Xhttp.send (""); returnXhttp.responsexml; } function ReadXml () {varxmldoc = Loadxmldoc ("Student.xml"); //get the specified node            varDivmsg = document.getElementById ("xmlmsg"); varmsg ="<table border= ' 1 ' id= ' mytable ' ><tr><th> name </th><th> sex </th><th> Age </th><tr>"; varnodes = Xmldoc.getelementsbytagname ("Stuinfo");  for(vari =0; i < nodes.length; i++) {msg+="<tr>"; Msg+="<td>"+ Nodes[i].getelementsbytagname ("Stuname")[0].firstchild.nodevalue +"</td>"; Msg+="<td>"+ Nodes[i].getelementsbytagname ("Stusex")[0].firstchild.nodevalue +"</td>"; Msg+="<td>"+ Nodes[i].getelementsbytagname ("Stuage")[0].firstchild.nodevalue +"</td>"; Msg+="</tr>"; } msg+="</table>"; Divmsg.innerhtml=msg; }    </script>"Form1"runat="Server"> <div> <input type="Button"Value="JS Read XML"onclick="ReadXml ()"/><br/> <div id="xmlmsg"> </div> </div> </form></body>

The above JS operation is mainly to avoid the use of childnodes (because Firefox childnodes[0] get "\ n" instead of the first child node we want, this can go to try, anyway, I have encountered this situation, so that can be compatible with IE, Firefox, Other browsers I didn't try.

JS reads XML file data and displays data in table (ie Firefox compatible)

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.