javascript 读取xml,写入xml 实现代码


Posted in Javascript onJuly 10, 2009

javascript 读取xml,写入xml 实现代码

添加数据 : javascript 读取xml,写入xml 实现代码

数据显示: javascript 读取xml,写入xml 实现代码

ClassModel.js源码 ::

ClassModel = 
{ 
    create : function() 
     { 
        return function() 
        { 
            this.construct.apply(this, arguments); 
        } 
     } 
} 
Extend = function(desc, src) 
    { 
        for(var c in src) 
        { 
            desc[c] = src[c]; 
        } 
        return desc; 
    } 
Object.prototype.extend = function(src) 
    { 
        return Extend.apply(this, [this, src]); 
    }

addData.js源码::
var insert = ClassModel.create(); 
var doc = new ActiveXObject("Msxml2.DOMDocument.3.0"); 
doc.load("books.xml"); 
var books; 
insert.prototype = 
{ 
construct : function(config) 
{ 
this.id = config.id; 
this.name = config.name; 
this.author = config.author; 
this.price = config.price; 
this.publisher = config.publisher; 
this.count = config.count; 
this.insertData(); 
}, 
insertData : function() 
{ var book = doc.createElement("book"); 
book.setAttribute("id", this.id); 
var name = doc.createElement("name"); 
var nameValue = doc.createTextNode(this.name); 
name.appendChild(nameValue); 
book.appendChild(name); 
var author = doc.createElement("author"); 
var authorValue = doc.createTextNode(this.author); 
author.appendChild(authorValue); 
book.appendChild(author); 
var price = doc.createElement("price"); 
var priceValue = doc.createTextNode(this.price); 
price.appendChild(priceValue); 
book.appendChild(price); 
var count = doc.createElement("count"); 
var countValue = doc.createTextNode(this.count); 
count.appendChild(countValue); 
book.appendChild(count); 
var publisher = doc.createElement("publisher"); 
var publisherValue = doc.createTextNode(this.publisher); 
publisher.appendChild(publisherValue); 
book.appendChild(publisher); 

if(doc.documentElement == null) 
{ 
books = doc.createElement("books"); 
books.appendChild(book); 
doc.appendChild(books); 
} 
else 
{ 
books = doc.documentElement; 
books.appendChild(book); 
} 
doc.save("books.xml"); 
} 
}

window.js源码::
var windows = ClassModel.create(); 
windows.prototype = 
{ 
construct : function(jsonObject) 
{ 
this.title = jsonObject.title; 
this.width = jsonObject.width; 
this.height = jsonObject.height; 
this.titleColor = jsonObject.titleColor; 
this.backgroundColor = jsonObject.backgroundColor; 
this.LwHeight = (document.body.clientHeight - this.width) / 2; //让div在屏幕的中间 
this.LwWidth = (document.body.clientWidth - this.height) / 2; //让div在屏幕的中间 
this.content = jsonObject.content; 
var loginWindow = this.createLoginBody(); 
var title = this.createLoginTitle(); 
loginWindow.appendChild(title); 
var cont = this.createContent(); 
loginWindow.appendChild(cont); 
document.body.appendChild(loginWindow); 
}, 
createLoginBody: function() //创建登陆框, 即整个框 
{ 
var loginWindow = document.createElement("div"); 
loginWindow.id = "dialog"; 
with(loginWindow.style) 
{ 
border = "1px solid white"; 
position = "absolute"; 
width = this.width + "px"; 
height = this.height + "px"; 
top = this.LwHeight + "px"; 
left = this.LwWidth + "px"; 
backgroundColor = this.backgroundColor; 
} 
return loginWindow; 
}, 
createLoginTitle:function() //创建 标题 即效果图的黑色标题 
{ 
var title = document.createElement("div"); 
var table = document.createElement("table"); 
var tbody = document.createElement("tbody"); 
var tr = document.createElement("tr"); 
var td_1 = document.createElement("td"); 
var td_2 = document.createElement("td"); 
var close = document.createElement("a"); 
close.onclick = function() 
{ 
document.body.removeChild(title.parentNode); 
} 
close.innerHTML = "X"; 
td_1.innerHTML = this.title; 
with(title.style) 
{ 
width = "100%"; 
height = this.height / 10 + "px"; 
backgroundColor = this.titleColor; 
} 
with(table.style) 
{ 
color = "white"; 
fontSize = "12pt"; 
width = "100%"; 
backgroundColor = this.titleColor; 
color = "red"; 
} 
td_2.style.textAlign = "right"; 
td_2.appendChild(close); 
tr.appendChild(td_1); 
tr.appendChild(td_2); 
tbody.appendChild(tr); 
table.appendChild(tbody); 
title.appendChild(table); 
return title; 
}, 
createContent : function() 
{ 
var div = document.createElement("div"); 
if(typeof(this.content) == 'string') 
{ 
div.innerHTML = this.content; 
}else 
{ 
div.appendChild(this.content); 
} 
with(div.style) 
{ 
paddingLeft = "80px"; 
paddingTop = "50px"; 
float = "left"; 
width = "100%"; 
} 
return div; 
} 
}

book_infor.js源码::
var doc = new ActiveXObject("Msxml2.DOMDocument.3.0"); 
doc.load("books.xml"); 
var query = ClassModel.create(); 
var v = 0; 
query.prototype = 
{ 
    construct : function() 
    { 
        this.bookInfor(); 
    }, 
    bookInfor : function() 
    { 
        var div = document.createElement("div"); 
        var root = doc.documentElement; 
        if(root == null) 
        { 
            div.innerHTML = "no data"; 
            document.body.appendChild(div); 
        }else 
        {          
        with(div.style) 
        { 
            marginLeft = "200px"; 
            overflow = "auto"; 
            border = "0px solid white"; 
            width = "605px"; 
        } 
        var table = document.createElement("table"); 
        table.cellSpacing = "0"; 
        with(table.style) 
        {     
            fontSize = "12pt"; 
            color = "white"; 
            border = "0px"; 
            width = "600px"; 
        } 
        var tbody = document.createElement("tbody"); 
        var trHead = document.createElement("tr"); 
        with(trHead.style) 
        { 
            height = "20px"; 
            backgroundColor = "Transparent"; 
        } 
        var tname = document.createElement("td"); 
        var tauthor = document.createElement("td"); 
        var tprice = document.createElement("td"); 
        var tCount = document.createElement("td"); 
        var tpublisher = document.createElement("td"); 
        tname.innerHTML = "名称"; 
        tauthor.innerHTML = "作者"; 
        tprice.innerHTML = "价格"; 
        tCount.innerHTML = "库存"; 
        tpublisher.innerHTML = "出版社"; 
        tname.style.borderBottom = "1px solid"; 
        tauthor.style.borderBottom = "1px solid"; 
        tprice.style.borderBottom = "1px solid"; 
        tCount.style.borderBottom = "1px solid"; 
        tpublisher.style.borderBottom = "1px solid"; 
        tname.style.width = "20%"; 
        tauthor.style.width = "20%"; 
        tprice.style.width = "20%"; 
        tCount.style.width = "20%"; 
        tpublisher.style.width = "20%"; 
        trHead.appendChild(tname); 
        trHead.appendChild(tauthor); 
        trHead.appendChild(tprice); 
        trHead.appendChild(tCount); 
        trHead.appendChild(tpublisher); 
        tbody.appendChild(trHead); 
     
        for(var c = 0; c < root.getElementsByTagName("book").length; c ++) 
        { 
            var roots = root.getElementsByTagName("book")[c]; 
            var id = roots.getAttribute("id"); 
            var name = roots.getElementsByTagName("name")[0].childNodes[0].nodeValue; 
            var author = roots.getElementsByTagName("author")[0].childNodes[0].nodeValue; 
            var price = roots.getElementsByTagName("price")[0].childNodes[0].nodeValue; 
            var count = roots.getElementsByTagName("count")[0].childNodes[0].nodeValue; 
            var publisher = roots.getElementsByTagName("publisher")[0].childNodes[0].nodeValue; 
            var tr = document.createElement("tr"); 
            with(tr.style) 
            { 
                backgroundColor = "Transparent"; 
            } 
            var tdName = document.createElement("td"); 
            var tdAuthor = document.createElement("td"); 
            var tdPrice = document.createElement("td"); 
            var tdCount = document.createElement("td"); 
            var tdPublisher = document.createElement("td"); 
             
            tdName.innerHTML = name; 
            tdAuthor.innerHTML = author; 
            tdPrice.innerHTML = price; 
            tdCount.innerHTML = count; 
            tdPublisher.innerHTML = publisher; 
     
            tdName.id = "tdName" + c; 
            tdAuthor.id = "tdAuthor" + c; 
            tdPrice.id = "tdPrice" + c; 
            tdCount.id = "tdCount" + c; 
            tdPublisher.id = "tdPublisher" + c; 
            tr.appendChild(tdName); 
            tr.appendChild(tdAuthor); 
            tr.appendChild(tdPrice); 
            tr.appendChild(tdCount); 
            tr.appendChild(tdPublisher); 
            tbody.appendChild(tr); 
            tdName.onmouseover = function(){ 
                document.body.style.cursor= "pointer"; 
                document.getElementById(this.id).style.backgroundColor = "darkred"; 
            } 
            tdName.onmouseout = function(){ 
                document.body.style.cursor= ""; 
                document.getElementById(this.id).style.backgroundColor = ""; 
            } 
            tdAuthor.onmouseover = function(){ 
                document.body.style.cursor= "pointer"; 
                document.getElementById(this.id).style.backgroundColor = "darkred"; 
            } 
            tdAuthor.onmouseout = function(){ 
                document.body.style.cursor= ""; 
                document.getElementById(this.id).style.backgroundColor = ""; 
            } 
            tdPrice.onmouseover = function(){ 
                document.body.style.cursor= "pointer"; 
                document.getElementById(this.id).style.backgroundColor = "darkred"; 
            } 
            tdPrice.onmouseout = function(){ 
                document.body.style.cursor= ""; 
                document.getElementById(this.id).style.backgroundColor = ""; 
            } 
            tdCount.onmouseover = function(){ 
                document.body.style.cursor= "pointer"; 
                document.getElementById(this.id).style.backgroundColor = "darkred"; 
            } 
            tdCount.onmouseout = function(){ 
                document.body.style.cursor= ""; 
                document.getElementById(this.id).style.backgroundColor = ""; 
            } 
            tdPublisher.onmouseover = function(){ 
                document.body.style.cursor= "pointer"; 
                document.getElementById(this.id).style.backgroundColor = "darkred"; 
            } 
            tdPublisher.onmouseout = function(){ 
                document.body.style.cursor= ""; 
                document.getElementById(this.id).style.backgroundColor = ""; 
            } 
        } 
        table.appendChild(tbody); 
        div.appendChild(table); 
        document.body.appendChild(div); 
         
    }     
}     
}    

Javascript 相关文章推荐
js 发个判断字符串是否为符合标准的函数
Apr 27 Javascript
解决Jquery load()加载GB2312页面时出现乱码的两种方案
Sep 10 Javascript
jquery选择器原理介绍($()使用方法)
Mar 25 Javascript
javascipt:filter过滤介绍及使用
Sep 10 Javascript
利用js编写响应式侧边栏
Sep 17 Javascript
浅谈js内置对象Math的属性和方法(推荐)
Sep 19 Javascript
浅谈pc端rem字体设置的问题
Aug 03 Javascript
浅谈js基础数据类型和引用类型,深浅拷贝问题,以及内存分配问题
Sep 02 Javascript
详解Node 定时器
Feb 26 Javascript
浅谈JavaScript 代码简洁之道
Jan 09 Javascript
vue如何在项目中调用腾讯云的滑动验证码
Jul 15 Javascript
Webpack的Loader和Plugin的区别
Nov 09 Javascript
jquery 1.3.2 IE8中的一点点的小问题解决方法
Jul 10 #Javascript
jquery Firefox3.5中操作select的问题
Jul 10 #Javascript
jQuery 版本的文本输入框检查器Input Check
Jul 09 #Javascript
window.onload 加载完毕的问题及解决方案(下)
Jul 09 #Javascript
window.onload 加载完毕的问题及解决方案(上)
Jul 09 #Javascript
最简单的jQuery程序 入门者学习
Jul 09 #Javascript
Jquery 组合form元素为json格式,asp.net反序列化
Jul 09 #Javascript
You might like
phpfans留言版用到的数据操作类和分页类
2007/01/04 PHP
php数据入库前清理 注意php intval与mysql的int取值范围不同
2010/12/12 PHP
php多文件上传实现代码
2014/02/20 PHP
PHP 文件上传后端处理实用技巧方法
2017/01/06 PHP
PHP下载文件函数与用法示例
2019/09/27 PHP
jquery Mobile入门—外部链接切换示例代码
2013/01/08 Javascript
javascript alert乱码的解决方法
2013/11/05 Javascript
利用JS进行图片的切换即特效展示图片
2013/12/03 Javascript
javascript实现数字验证码的简单实例
2014/02/10 Javascript
jquery制作弹窗提示窗口代码分享
2014/03/02 Javascript
js判断游览器类型及版本号的代码
2014/05/11 Javascript
基于JavaScript实现动态添加删除表格的行
2016/02/01 Javascript
jQuery Easyui DataGrid点击某个单元格即进入编辑状态焦点移开后保存数据
2016/08/15 Javascript
详解VueJs异步动态加载块
2017/03/09 Javascript
vue.js异步上传文件前后端实现代码
2017/08/22 Javascript
使用ajax的post同步执行(实现方法)
2017/12/21 Javascript
vue模式history下在iis中配置流程
2019/04/17 Javascript
vue watch关于对象内的属性监听
2019/04/22 Javascript
[38:41]2014 DOTA2国际邀请赛中国区预选赛 LGD VS CNB
2014/05/22 DOTA
python网络编程学习笔记(六):Web客户端访问
2014/06/09 Python
在Python程序中操作MySQL的基本方法
2015/07/29 Python
python的pandas工具包,保存.csv文件时不要表头的实例
2018/06/14 Python
python使用minimax算法实现五子棋
2019/07/29 Python
python实现拉普拉斯特征图降维示例
2019/11/25 Python
离线状态下在jupyter notebook中使用plotly实例
2020/04/24 Python
详解PyQt5中textBrowser显示print语句输出的简单方法
2020/08/07 Python
python 星号(*)的多种用途
2020/09/21 Python
HTML5头部标签的一些常用信息小结
2016/10/23 HTML / CSS
周仰杰(JIMMY CHOO)英国官方网站:闻名世界的鞋子品牌
2018/10/28 全球购物
新媒传信软件测试面试题
2013/02/24 面试题
工作会议欢迎词
2014/01/16 职场文书
《李时珍夜宿古寺》教学反思
2014/04/09 职场文书
广场舞大赛策划方案
2014/05/31 职场文书
激励员工的口号
2014/06/16 职场文书
2014财务人员自我评价范文
2014/09/21 职场文书
vue中使用mockjs配置和使用方式
2022/04/06 Vue.js