Home  >  Article  >  Web Front-end  >  XML operation implementation code in javascript_javascript skills

XML operation implementation code in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:59:141245browse

JavaScript side:

Copy code The code is as follows:

//Initialization page
function init () {
var ary = JSONToArray(XMLReader("node","content.dibi"));
var divtoc = document.getElementById("div_toc");
pageCount = ary.length;
for(k = 0; k < ary.length; k ){
obj = eval('(' ary[k] ')');
divtoc.innerHTML = ""
obj.label "
" ";
}

page = 1;
changeImage(page);

var pageManager = document.getElementById("div_page");
pageManager.innerHTML = "Previous page " "
"Next page


var ary2 = JSONToArray(XMLReader("meta","content.dibi"));
var divmeta = document.getElementById("div_meta");
var styStr = " "
for(l = 0; l < ary2.length; l ){
obj2 = eval('(' ary2[l] ')');
styStr = "< ;tr>";
}
divmeta.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.documentElement;
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. Support IE, 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; //Total number of pages

/ /Change page image based on click
function changeImage(page1){
page = page1;
var divimg = document.getElementById("div_img");
divimg.innerHTML = "";
}

//Previous page and next page
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 side:
Copy the code The code is as follows:




< ;script language="javascript" type="text/javascript" charset='gbk' src="xmlhelper.js">









XML side: omitted.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn