javascript中xml操作实现代码_javascript技巧
程序员文章站
2022-06-04 12:44:40
...
JavaScript 端:
//初始化页面
function init() {
var ary = JSONToArray(XMLReader("node","content.dibi"));
var divtoc = document.getElementById("div_toc");
pageCount = ary.length;
for(k = 0; k obj = eval('(' + ary[k] + ')');
divtoc.innerHTML += ""
+ obj.label + " ";
}
page = 1;
changeImage(page);
var pageManager = document.getElementById("div_page");
pageManager.innerHTML = "上一页 "
+ "下一页
";
var ary2 = JSONToArray(XMLReader("meta","content.dibi"));
var divmeta = document.getElementById("div_meta");
var styStr = ""
";
}
//解析.dibi 文件。
function XMLReader(key,fileName) {
var parse = BrowserValidator();
parse.load(fileName);
var json = "";
try{
var dom = parse.documentElement;
var attrLength = 0;
for (i = 0; i attrLength = dom.getElementsByTagName(key)[i].attributes.length;
objMsg = ",{";
for(j = 0; 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;
}
//判断浏览器类型。支持 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; //当前页
var pageCount; //总页数
//根据点击改变页面图片
function changeImage(page1){
page = page1;
var divimg = document.getElementById("div_img");
divimg.innerHTML = "";
}
//上页及下页
function changePage(flag){
if(flag == 0 && page > 1){
page--;
}
if(flag == 1 && page page++;
}
changeImage(page);
}
//将 JSON 数据转换成数组
function JSONToArray(json){
return json.replace(new RegExp("},", "g"), "}|").split("|");
}
HTML 端:
XML 端:略。
复制代码 代码如下:
//初始化页面
function init() {
var ary = JSONToArray(XMLReader("node","content.dibi"));
var divtoc = document.getElementById("div_toc");
pageCount = ary.length;
for(k = 0; k obj = eval('(' + ary[k] + ')');
divtoc.innerHTML += ""
+ obj.label + " ";
}
page = 1;
changeImage(page);
var pageManager = document.getElementById("div_page");
pageManager.innerHTML = "上一页 "
+ "下一页
";
var ary2 = JSONToArray(XMLReader("meta","content.dibi"));
var divmeta = document.getElementById("div_meta");
var styStr = "
" + obj2.name + " | " + obj2.content + " |
}
//解析.dibi 文件。
function XMLReader(key,fileName) {
var parse = BrowserValidator();
parse.load(fileName);
var json = "";
try{
var dom = parse.documentElement;
var attrLength = 0;
for (i = 0; i attrLength = dom.getElementsByTagName(key)[i].attributes.length;
objMsg = ",{";
for(j = 0; 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;
}
//判断浏览器类型。支持 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; //当前页
var pageCount; //总页数
//根据点击改变页面图片
function changeImage(page1){
page = page1;
var divimg = document.getElementById("div_img");
divimg.innerHTML = "";
}
//上页及下页
function changePage(flag){
if(flag == 0 && page > 1){
page--;
}
if(flag == 1 && page page++;
}
changeImage(page);
}
//将 JSON 数据转换成数组
function JSONToArray(json){
return json.replace(new RegExp("},", "g"), "}|").split("|");
}
HTML 端:
复制代码 代码如下:
XML 端:略。
推荐阅读
-
web在html中引用JavaScript代码的实现(小程序在xwml中实现)
-
PHP实现Javascript中的escape及unescape函数代码分享
-
ruby、javascript、php中的观察者模式实现代码
-
javascript返回上一页操作代码实现
-
在C#中调用VBScript、javascript等脚本的实现代码
-
JavaScript中数组常见操作技巧
-
JavaScript中的FileReader图片预览上传功能实现代码
-
在PHP中实现Javascript的escape()函数代码
-
JavaScript在ASP页面中实现掩码文本框效果代码
-
原生JavaScript将账号密码保存到cookie中(代码实现)