Javascript code for xml operations _ javascript skills

Source: Internet
Author: User
I haven't written it for a long time. I feel that HTML5 is still just a way of playing tickets today. I hope w3c standards can be introduced earlier. Do not let the compatibility and support of various browsers end up with JavaScript:

The Code is as follows:


// Initialization page
Function init (){
Var ary = JSONToArray (XMLReader ("node", "content. dibi "));
Var ptoc = document. getElementById ("p_toc ");
PageCount = ary. length;
For (k = 0; k <ary. length; k ++ ){
Obj = eval ('+ ary [k] + ')');
Ptoc. innerHTML + = ""
+ Obj. label + "";
}

Page = 1;
ChangeImage (page );

Var pageManager = document. getElementById ("p_page ");
PageManager. innerHTML = "Previous Page"
+ "Next page

";

Var ary2 = JSONToArray (XMLReader ("meta", "content. dibi "));
Var pmeta = document. getElementById ("p_meta ");
Var styStr ="






"For (l = 0; l <ary2.length; l ++ ){Obj2 = eval ('+ ary2 [l] + ')');StyStr + =" ";}Pmeta. innerHTML = styStr +"
"+ Obj2.name +" "+ Obj2.content +"
";
}

// Parse the. dibi file.
Function XMLReader (key, fileName ){
Var parse = BrowserValidator ();
Parse. load (fileName );
Var json = "";
Try {
Var dom = parse.doc umentElement;
Var attrLength = 0;
For (I = 0; I <dom. getElementsByTagName (key). length; I ++ ){
AttrLength = dom. getElementsByTagName (key) [I]. attributes. length;
ObjMsg = ",{";
For (j = 0; j <attrLength; j ++ ){
ObjMsg + = "'" + dom. getElementsByTagName (key) [I]. attributes [j]. name
+ "':'" + Dom. getElementsByTagName (key) [I]. attributes [j]. value + "',";
}
ObjMsg = objMsg. substring (0, objMsg. length-1 );
Json + = objMsg + "}";
}
Json = json. substring (1 );
} Catch (e ){}
Return json;
}

// Determine the browser type. Supports IE and fireFox.
Function BrowserValidator (){
Var result;
If (! Window. DOMParser & window. ActiveXObject ){
Result = new ActiveXObject ("Microsoft. XMLDOM ");
Result. async = false;
}
Else if (document. implementation & document. implementation. createDocument ){
Result = document. implementation. createDocument ("", "", null );
Result. async = false;
}
Return result;
}

Var page = 1; // current page
Var pageCount; // the total number of pages.

// Click to change the page Image
Function changeImage (page1 ){
Page = page1;
Var pimg = document. getElementById ("p_img ");
Pimg. innerHTML = "";
}

// Previous and next pages
Function changePage (flag ){
If (flag = 0 & page> 1 ){
Page --;
}
If (flag = 1 & page <pageCount ){
Page ++;
}
ChangeImage (page );
}


// Convert JSON data into an array
Function JSONToArray (json ){
Return json. replace (new RegExp ("},", "g"), "} |"). split ("| ");
}


HTML:

The Code is as follows:





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.