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);

}    
}    
}

时间: 2009-07-07

JS解析XML的实现代码

JS代码: 复制代码 代码如下: <script language="javascript" type="text/javascript"> //需要读取的xml文件 var uRl = "jsReadXml.xml"; var xmlDoc; //初始化,给上述定义变量赋值 // function showcurcity(){ if(window.ActiveXObject) { xmlDoc=new ActiveXObject(&

JavaScript将XML转成JSON的方法

本文实例讲述了JavaScript将XML转成JSON的方法.分享给大家供大家参考.具体方法如下: 1. JavaScript代码如下: 复制代码 代码如下: // Changes XML to JSON function xmlToJson(xml) {     // Create the return object     var obj = {};     if (xml.nodeType == 1) { // element         // do attributes       

xml转json的js代码

复制代码 代码如下: function xmlToJson(xml) { // Create the return object var obj = {}; if (xml.nodeType == 1) { // element // do attributes if (xml.attributes.length > 0) { obj["@attributes"] = {}; for (var j = 0; j < xml.attributes.length; j++) {

JavaScript 解析读取XML文档 实例代码

JavaScript解析读取XML文件,主要就是加载并解析XML文件,然后就可以测试解析的XML文件的内容,打印输出来. 在线演示:http://demo.jb51.net/js/2012/readxml/注:测试的时候需要在网站中测试,iis或apache中,注意不要本地双击运行测试index.htm 复制代码 代码如下: <html> <head> <title>我们</title> <script type="text/javascri

JSON与XML的区别对比及案例应用

1.定义介绍 (1).XML定义 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. XML使用DTD(document type definition)文档类型定义来组织数据;格式统一,跨平台和语言,早已成为业界公认的标准. XML是标准通用标记语言 (SGML) 的子集,非常适合 Web 传输.XML 提供统一的方法来描述和交换独立于应用

json跟xml的对比分析

·可读性 JSON和XML的可读性可谓不相上下,一边是建议的语法,一边是规范的标签形式,很难分出胜负. ·可扩展性 XML天生有很好的扩展性,JSON当然也有,没有什么是XML能扩展,JSON不能的. ·编码难度 XML有丰富的编码工具,比如Dom4j.JDom等,JSON也有json.org提供的工具,但是JSON的编码明显比XML容易许多,即使不借助工具也能写出JSON的代码,可是要写好XML就不太容易了. ·解码难度 XML的解析得考虑子节点父节点,让人头昏眼花,而JSON的解析难度几乎为

如何在JS中实现相互转换XML和JSON

开发中有时候会遇到XML和JSON相互转换,要求在JS中使用,网上找了好多,竟然每一个好用的,要么缺胳膊少腿,要么词不达意,太没天理了,果断自己实现一个. JSON与XML的区别比较 1.定义介绍 (1).XML定义 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. XML使用DTD(document type definition)文档类

JQuery解析HTML、JSON和XML实例详解

1.HTML 有的时候会将一段HTML片段保存在HTML文件中,在另外的主页面直接读取该HTML文件,然后解析里面的HTML代码片段融入到主页面中. fragment.html文件,其内容: 复制代码 代码如下: <div>hello Jquery</div> 在主页面 Test.html中解析代码 复制代码 代码如下: $("#a1").click(function(){     $("#div2").load('fragment.html

javascript解析xml字符串的函数

但是是XML字符串,则在两种浏览器下就会有所不同,IE下可以直接使用LoadXML方法解析XML字符串,而在FF下则要使用DOMParser 对象的parseFromString() 方法即 var oParser=new DOMParser(); xmlDoc=oParser.parseFromString(xmlStr,"text/xml"); 为了在两种浏览器中能通用,我想到了javascritp的发生异常的处理方式,就是try...catch... 复制代码 代码如下: fun

js实现的xml对象转json功能示例

本文实例讲述了js实现的xml对象转json功能.分享给大家供大家参考,具体如下: 支持无限级别xml结构对象转json,并且支持任意标签属性转json(兼容ie8等浏览器) xml字符串转xml对象: function loadXml(str) { if (str == null) { return null; } var doc = str; try{ doc = createXMLDOM(); doc.async = false; doc.loadXML(str); }catch(e){

javascript 读取XML数据,在页面中展现、编辑、保存的实现

首先考虑用什么方法做,考虑到三个方式:1.C#拼HTML构造table,修改和保存通过Ajax实现.2.XML+XSL,展现和修改用两个XSL文件来做,Ajax修改.保存XML.3.GridView控件. 经过细致考虑,首先第三方案GridView控件满足不了需求,因为XML格式多样,可能涉及到很多的行.列合并和行.列表头合并.第一方案太麻烦,坐起来是细致活和体力活,需求变动后不好修改.所以选择第二方案.开始学习XPath.XSLT.AJAX用js异步调用一般处理文件(ashx)的方式. 1.实

JS解析XML文件和XML字符串详解

JS解析XML文件 <script type='text/javascript'> loadXML = function(xmlFile){ var xmlDoc=null; //判断浏览器的类型 //支持IE浏览器 if(!window.DOMParser && window.ActiveXObject){ var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.

JSON与XML优缺点对比分析

1. 定义介绍 1.1 XML定义 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. XML使用DTD(document type definition)文档类型定义来组织数据;格式统一,跨平台和语言,早已成为业界公认的标准. XML是标准通用标记语言 (SGML) 的子集,非常适合 Web 传输.XML 提供统一的方法来描述和交换独立于应