JS reads and outputs xml file nodes and attributes (compatible with IE and FF)

Source: Internet
Author: User
JS reads and outputs xml file nodes and attributes (compatible with IE and FF)

 

This is difficult for me to find, but I still find it.

Haidong cannot hide it on its own. I sent it out and shared it. ^ _ ^ |

The book.html code is as follows:

++ ++

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Script language = "javascript">
Var xmlDoc;
Var browse = "";
Function loadXML ()
{
Var fileRoute = "books. xml"
If (window. ActiveXObject)
{
XmlDoc = new ActiveXObject ('msxml2. domdocument ');
XmlDoc. async = false;
XmlDoc. load (fileRoute );
Browse = "ie ";
}
Else if (document. implementation & document. implementation. createDocument)
{
XmlDoc = document. implementation. createDocument ('','', null );
XmlDoc. load (fileRoute );
Browse = "ff ";
}
Else
{
Alert ('not compatible with this browser! ');
}
}

Function getmessage ()
{
Var msg = '<table border = "1" cellspacing = "0" cellpadding = "0" width = "500"> ';
Msg + = '<tr> <td width = "90"> </td> <td width = "100"> book no. </td> <td width = "100"> category No. </td> <td width = "110"> book name </td> <td width = "100"> author </td> </tr>'
If (browse = "ff ")
{
Var cNodes = xmlDoc. getElementsByTagName ("book ");
For (j = 0; j <cNodes. length; j ++)
{
Var bookID = xmlDoc. getElementsByTagName ("book") [j]. getAttribute ("id ");
Var sortID = xmlDoc. getElementsByTagName ("book") [j]. getAttribute ("sortID ");
Var bookTitle = xmlDoc. getElementsByTagName ("title") [j]. childNodes [0]. nodeValue;
Var bookAuthor = xmlDoc. getElementsByTagName ("author") [j]. childNodes [0]. nodeValue;
Msg + = '<tr> <td>' + j + '</td> <td>' + bookID + '</td> <td width = "100">' + sortID + '</td> <td width = "190">' + bookTitle + '</td> <td width = "120">' + bookAuthor + '</ td> </tr>'
}

}
Else if (browse = "ie ")
{
Var state = xmlDoc. readyState;
If (state = 4)
{
Var oNodes = xmlDoc. selectNodes ("// books/book ");
For (j = 0; j <oNodes. length; j ++)
{
Var bookID = oNodes [j]. getAttribute ("id ");
Var sortID = oNodes [j]. getAttribute ("sortID ");
Var bookTitle = oNodes [j]. childNodes [0]. text;
Var bookAuthor = oNodes [j]. childNodes [1]. text;
Msg + = '<tr> <td>' + j + '</td> <td>' + bookID + '</td> <td width = "100">' + sortID + '</td> <td width = "190">' + bookTitle + '</td> <td width = "120">' + bookAuthor + '</ td> </tr>'
}
}
}
Msg + = '</table> ';
// Alert (msg)
Document. getElementById ("bookList"). innerHTML = msg;
}

</Script>
</Head>
<Body onload = "loadXML ();">
<Div id = "bookList" style = "width: 500px;">
</Div>
<Table border = "0" cellspacing = "0" cellpadding = "0" width = "500">
<Tr>
<Td> & nbsp; </td>
<Td> & nbsp; </td>
<Td> & nbsp; </td>
<Td> </td>
</Tr>
</Table>
<Input name = "button" type = "button" onclick = "getmessage ()" value = "onclick"/>
</Body>
</Html>

++ ++

The books. xml Code is as follows:

++ ++

<? Xml version = "1.0" encoding = "gb2312"?>
<Books>
<Book id = "4" sortID = "a4">
<Title> author4aa </title>
<Author> author4 </author>
</Book>
<Book id = "5" sortID = "a5">
<Title> author55 </title>
<Author> author5 </author>
</Book>
<Book id = "6" sortID = "a6">
<Title> booktitle </title>
<Author> author6 </author>
</Book>
<Book id = "7" sortID = "a7">
<Title> booktitle </title>
<Author> author7 </author>
</Book>
<Book id = "8" sortID = "a8">
<Title> booktitle </title>
<Author> author8 </author>
</Book>
<Book id = "15" sortID = "a15">
<Title> author155 </title>
<Author> author15 </author>
</Book>
</Books>

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.