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 相关文章推荐
优化网页之快速的呈现我们的网页
Jun 29 Javascript
解析js如何获取当前url中的参数值并复制给input
Jun 23 Javascript
javascript中的正则表达式使用详解
Aug 30 Javascript
Bootstrap每天必学之缩略图与警示窗
Nov 29 Javascript
AngularJS深入探讨scope,继承结构,事件系统和生命周期
Nov 02 Javascript
jquery实现简单的瀑布流布局
Dec 11 Javascript
jQuery插件autocomplete使用详解
Feb 04 Javascript
Angularjs使用指令做表单校验的方法
Mar 31 Javascript
详解从买域名到使用pm2部署node.js项目全过程
Mar 07 Javascript
vue利用v-for嵌套输出多层对象,分别输出到个表的方法
Sep 07 Javascript
解决ele ui 表格表头太长问题的实现
Nov 13 Javascript
详解js创建对象的几种方式和对象方法
Mar 01 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
咖啡豆分级制度 咖啡豆等级分类 咖啡豆是按口感分类的吗?
2021/03/05 新手入门
PHP5/ZendEngine2的改进
2006/10/09 PHP
php学习笔记 数组的常用函数
2011/06/13 PHP
基于php冒泡排序算法的深入理解
2013/06/09 PHP
smarty模板引擎之配置文件数据和保留数据
2015/03/30 PHP
PHP简单读取PDF页数的实现方法
2016/07/21 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
2017/08/31 PHP
Jquery中LigerUi的弹出编辑框(实现方法)
2013/07/09 Javascript
JavaScript中的数组操作介绍
2014/12/30 Javascript
jquery实现简洁文件上传表单样式
2015/11/02 Javascript
使用json来定义函数,在里面可以定义多个函数的实现方法
2016/10/28 Javascript
JS日程管理插件FullCalendar中文说明文档
2017/02/06 Javascript
详解Javascript百度地图接口开发文档中的类和方法
2017/02/07 Javascript
js实现瀑布流效果(自动生成新的内容)
2017/03/16 Javascript
ubuntu编译nodejs所需的软件并安装
2017/09/12 NodeJs
详解element-ui中form验证杂记
2019/03/04 Javascript
如何实现js拖拽效果及原理解析
2020/05/08 Javascript
[01:33]PWL开团时刻DAY2-开雾与反开雾
2020/10/31 DOTA
在Python中操作列表之List.pop()方法的使用
2015/05/21 Python
python提取照片坐标信息的实例代码
2019/08/14 Python
使用Python函数进行模块化的实现
2019/11/15 Python
Python FtpLib模块应用操作详解
2019/12/12 Python
详解pyinstaller生成exe的闪退问题解决方案
2020/06/19 Python
详解anaconda安装步骤
2020/11/23 Python
HTML5 Canvas实现烟花绽放特效
2016/03/02 HTML / CSS
日本最大美瞳直送网:Morecontact(中文)
2019/04/03 全球购物
历史学专业推荐信
2013/11/06 职场文书
物业管理计划书
2014/01/10 职场文书
30年同学聚会邀请函
2014/01/25 职场文书
学生鉴定评语大全
2014/05/05 职场文书
文化建设工作方案
2014/05/12 职场文书
工会优秀工作者事迹
2014/08/17 职场文书
中国文明网2015年“向国旗敬礼”活动网上签名寄语
2015/09/24 职场文书
2016年教师学习廉政准则心得体会
2016/01/20 职场文书
Golang 字符串的常见操作
2022/04/19 Golang
Flink 侧流输出源码示例解析
2022/09/23 Servers