源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 621|回复: 13

[XML/RSS] JavaScript生成xml

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2006-10-10 00:00:00 | 显示全部楼层 |阅读模式
[U]复制代码[/U] 代码如下:
function XMLWriter()
{
    this.XML=[];
    this.Nodes=[];
    this.State="";
    this.FormatXML = function(Str)
    {
        if (Str)
            return Str.replace(/&/g, "&").replace(/\"/g, """).replace(//g, ">");
        return ""
    }
    this.BeginNode = function(Name)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.State="beg";
        this.Nodes.push(Name);
        this.XML.push("");
            this.Nodes.pop();
        }
        else if (this.Nodes.length>0)
            this.XML.push("");
        this.State="";
    }
    this.Attrib = function(Name, Value)
    {
        if (this.State!="beg" || !Name) return;
        this.XML.push(" "+Name+"=\""+this.FormatXML(Value)+"\"");
    }
    this.WriteString = function(Value)
    {
        if (this.State=="beg") this.XML.push(">");
        this.XML.push(this.FormatXML(Value));
        this.State="";
    }
    this.Node = function(Name, Value)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.XML.push((Value=="" || !Value)?"":""+this.FormatXML(Value)+"");
        this.State="";
    }
    this.Close = function()
    {
        while (this.Nodes.length>0)
            this.EndNode();
        this.State="closed";
    }
    this.ToString = function(){return this.XML.join("");}
}
XMLWriter 有以下几个方法:
BeginNode (Name)  
EndNode ()  
Attrib (Name, Value)  
WriteString (Value)  
Node (Name, Value)  
Close ()  
ToString ()  
BeginNode 输出一个标签:
XML.BeginNode(“Foo”);
XML.BeginNode(“Foo”);
XML.Attrib(“Bar”, “Some Value”);
WriteString 方法:
XML.Node(“MyNode”, “My Value”);
//Produces: My Value
XML.BeginNode(“Foo”);
XML.WriteString(“Hello World”);
XML.EndNode();
//Produces Hello World
Node 方法:
XML.EndNode();
//Produces:  
  
eg:
[U]复制代码[/U] 代码如下:
function WriteTest()
        {
            try
            {
                var XML=new XMLWriter();
                XML.BeginNode("Example");
                XML.Attrib("SomeAttribute", "And Some Value");
                XML.Attrib("AnotherAttrib", "...");
                XML.WriteString("This is an example of the JS XML WriteString method.");
                XML.Node("Name", "Value");
                XML.BeginNode("SubNode");
                XML.BeginNode("SubNode2");
                XML.EndNode();
                XML.BeginNode("SubNode3");
                XML.WriteString("Blah blah.");
                XML.EndNode();
                XML.Close(); // Takes care of unended tags.
                // The replace in the following line are only for making the XML look prettier in the textarea.
                document.getElementById("ExampleOutput").value=XML.ToString().replace(/This is an example of the JS XML WriteString method.
Value



Blah blah.
回复

使用道具 举报

8

主题

1万

回帖

52

积分

注册会员

Rank: 2

积分
52
发表于 2022-9-16 06:46:06 | 显示全部楼层
看到这帖子真是高兴!
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

176

积分

注册会员

Rank: 2

积分
176
发表于 2022-12-12 04:50:23 | 显示全部楼层
的谁vdvdsvdsvdsdsv
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-2-20 11:29:01 | 显示全部楼层
哦哦哦ijhhsdj
回复 支持 反对

使用道具 举报

15

主题

1万

回帖

122

积分

注册会员

Rank: 2

积分
122
发表于 2023-3-20 06:17:04 | 显示全部楼层
终于找到了,我擦
回复 支持 反对

使用道具 举报

27

主题

1万

回帖

331

积分

中级会员

Rank: 3Rank: 3

积分
331
发表于 2023-3-21 19:56:14 | 显示全部楼层
啊啊啊啊啊啊啊啊啊啊啊啊啊啊
回复 支持 反对

使用道具 举报

3

主题

1万

回帖

294

积分

中级会员

Rank: 3Rank: 3

积分
294
发表于 2023-7-2 15:52:05 | 显示全部楼层
额UI废物iuhfujewfiewnnfen
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-11-3 07:59:05 | 显示全部楼层
额UI废物iuhfujewfiewnnfen
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

69

积分

注册会员

Rank: 2

积分
69
发表于 2024-6-23 02:12:57 | 显示全部楼层
额头额定法国队是范德萨
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-7-19 14:25:45 | 显示全部楼层
撒旦撒旦撒擦擦擦擦
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-21 08:14 , Processed in 0.109522 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表