JavaScript reads XML file

Source: Internet
Author: User
  Core part <%@ page language= "Java"  import= "java.util.*"  pageencoding= "GBK"%> < script language= "JavaScript" >     //create DOM objects     function  CreateDocument ()  {        var aversions = ["MSXML2 . domdocument.5.0 ",                           "MSXML2. domdocument.4.0 ",                           "MSXML2. domdocument.3.0 ",                           "MSXML2. DOMDocument ",                          "Microsoft.XMLDOM"                          ];           for  (var i = 0; i  < aversions.length; i++)  {              try {                  var oxmldom = new activexobject (Aversions[i]);                  return  oXmlDom;              } catch  (Oerror)   {          }          }         throw new error ("MSxml is not installed. "); &NBSP;&NBSP;&NBSP;&NBSP} function init ()  {    var oXmlDom =  CreateDocument ();     oXmlDom.async = true;     oXmlDom.onreadystatechange = function  ()  {         if  (oxmldom.readystate == 4)  {                 if  (oxmldom.parseerror.errorcode ==  0)  {                     parsebookinfo (Oxmldom);                 }else{                      var str =  "an error occurred!! /n " +                       "description: "  + oXmlDom.parseError.reason +  "/n"  +                       " file:  " + oXmlDom.parseError.url + "/n " +                       "line: "  +  oxmldom.parseerror.line +  "/n"  +                       "line position: "  +  oxmldom.parseerror.linepos +  "/n"  +                       "source code: "  +  OXmlDom.parseError.srcText;   &nbSp;                 alert (str);                 }                     }      };     oxmldom.load ("books.xml"); }        //Document Parsing     function parsebookinfo (OXMLDOM)  {        var oRoot = oXmlDom.documentElement;         var ofragment = document.createdocumentfragment ();         var abooks = oroot.getelementsbytagname ("book") ;         //traversal generates each information         for   (Var i = 0; i < abooks.length; i++)  {             var sisbn = abooks[i].getattribute ("ISBN");             var sAuthor, sTitle,  Spublisher;             var oCurrentChild =  Abooks[i].firstchild;             do {                 switch  (Ocurrentchild.tagname)  {                     case  "title":stitle = ocurrentchild.text;            break;                     case  "Author":sauthor = ocurrentchild.text;          break;                      case  Publisher:spublisher = ocurrentchild.text;   break;                      default: break;                 }              }while (ocurrentchildocurrentchild =  ocurrentchild.nextsibling);                           //---------------Generate display page--------------             //generates each XML elementThe basic container             var divcontainer =  document.createelement ("div");             var imgBookCover =  Document.createelement ("img");             var divContent =  Document.createelement ("div");             var sOdd =  (i %  2)? "": "-odd";             divContainer.className =  " Bookcontainer " + sOdd;                           //Cover Information              imgBookCover.src =  "images/"  + sIsbn +  ". jpg";             imgbookcover.classname =  "BookCover";             divcontainer.appendchild (ImgBookCover) ;             //title              var h3title = document.createelement ("H3");             h3title.appendchild ( document.createTextNode (Stitle));             divcontent.appendchild (H3Title);             //author              divcontent.appendchild (document.createTextNode ("Written by:   " + sauthor));             divcontent.appendchild (DocumeNt.createelement ("BR"));             divcontent.appendchild ( document.createTextNode ("isbn: #" &NBSP;+&NBSP;SISBN));             //Publishing House              var divpublisher = document.createelement ("div");             divPublisher.className =  " Bookpublisher ";             divpublisher.appendchild ( document.createTextNode ("published by: "  + spublisher));             divcontent.appendchild (DivPublisher);             //Assembly              divContent.className =  "Bookcontent";            divcontainer.appendchild (divContent);             ofragment.appendchild (DivContainer);                           document.body.appendchild (ofragment); &NBSP;&NBSP;&NBSP;&NBSP}} </script>           <! doctype html public  "-//w3c//dtd xhtml 1.1//en " Http://www.w3.org/TR/xhtml11/DTD /xhtml11.dtd ">

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.