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检查页面上有无重复id的实现代码
Jul 17 Javascript
jquery移除、绑定、触发元素事件使用示例详解
Apr 10 Javascript
JavaScript中获取样式的原生方法小结
Oct 08 Javascript
JavaScript中判断变量是数组、函数或是对象类型的方法
Feb 25 Javascript
jQuery Ajax调用WCF服务详细教程
Mar 31 Javascript
jQuery中deferred对象使用方法详解
Jul 14 Javascript
使用JS中的exec()方法构造正则表达式验证
Aug 01 Javascript
jQuery中Find选择器用法示例
Sep 21 Javascript
如何在Angular.JS中接收并下载PDF
Nov 26 Javascript
jQuery插件echarts实现的去掉X轴、Y轴和网格线效果示例【附demo源码下载】
Mar 04 Javascript
JavaScript中重名的函数与对象示例详析
Sep 28 Javascript
ES6 更易于继承的类语法的使用
Feb 11 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
Windows2003 下 MySQL 数据库每天自动备份
2006/12/21 PHP
php实现的简单压缩英文字符串的代码
2008/04/24 PHP
PHP define函数的使用说明
2008/08/27 PHP
从康盛产品(discuz)提取出来的模板类
2011/06/28 PHP
php获取从百度搜索进入网站的关键词的详细代码
2014/01/08 PHP
php禁止某ip或ip地址段访问的方法
2015/02/25 PHP
PHP实现实时生成并下载超大数据量的EXCEL文件详解
2017/10/23 PHP
thinkPHP框架自动填充原理与用法分析
2018/04/03 PHP
PHP使用pdo连接access数据库并循环显示数据操作示例
2018/06/05 PHP
PHP堆栈调试操作简单示例
2018/06/15 PHP
TP3.2.3框架使用CKeditor编辑器在页面中上传图片的方法分析
2019/12/31 PHP
jQuery在vs2008及js文件中的无智能提示的解决方法
2010/12/30 Javascript
浅析Cookie中的Path与domain
2013/12/18 Javascript
JS实现动态生成表格并提交表格数据向后端
2020/11/25 Javascript
jQuery+Ajax实现限制查询间隔的方法
2016/06/07 Javascript
有关suggest快速删除后仍然出现下拉列表的bug问题
2016/12/02 Javascript
Vue.js实现表格动态增加删除的方法(附源码下载)
2017/01/20 Javascript
微信小程序之picker日期和时间选择器
2017/02/09 Javascript
jQuery插件FusionWidgets实现的AngularGauge图效果示例【附demo源码】
2017/03/23 jQuery
详解vue+vueRouter+webpack的简单实例
2017/06/17 Javascript
JavaScript实现简单的树形菜单效果
2017/06/23 Javascript
基于ES6 Array.of的用法(实例讲解)
2017/09/05 Javascript
微信小程序自定义组件实现tabs选项卡功能
2018/07/14 Javascript
JS构造一个html文本内容成文件流形式发送到后台
2018/07/31 Javascript
Node如何后台数据库使用增删改查功能
2019/11/21 Javascript
JavaScript之Blob对象类型的具体使用方法
2019/11/29 Javascript
react项目从新建到部署的实现示例
2021/02/19 Javascript
[01:54]胎教DOTA2 准妈妈玩家现身中国区预选赛
2016/06/26 DOTA
python pickle 和 shelve模块的用法
2013/09/16 Python
用Python编程实现语音控制电脑
2014/04/01 Python
python中dir函数用法分析
2015/04/17 Python
在Python3 numpy中mean和average的区别详解
2019/08/24 Python
python 实现将Numpy数组保存为图像
2020/01/09 Python
phpquery中文手册
2021/03/18 PHP
金融专业个人求职信
2013/09/22 职场文书
医院护士的求职信
2014/01/03 职场文书