用javascript从表单中生成xml

本文以一个简单的登录界面演示,有三个文件,login.html, XMLWriter.js, login.js。

 

 

login.html

 

<html>
<head>
<script  src = "XMLWriter.js" ></script>
<script  src = "login.js" ></script>
</head>
<body>
<form>
用户名:<input id = "uname" type = "text"></input>
<br/>
密   码:<input id = "pword" type = "password"></input>
<br/>
<button id = "aa">submit</button>
</form>
<br/>
</body>
</html>

 

 

XMLWriter.js

 

 

function XMLWriter()
{
    this.XML=['<?xml version="1.0" encoding="UTF-8"?>'];
    this.Nodes=[];
    this.State="";
    this.FormatXML = function(Str)
    {
        if (Str)
            return Str.replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        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("<"+Name);
    }
    this.EndNode = function()
    {
        if (this.State=="beg")
        {
            this.XML.push("/>");
            this.Nodes.pop();
        }
        else if (this.Nodes.length>0)
            this.XML.push("</"+this.Nodes.pop()+">");
        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)?"<"+Name+"/>":"<"+Name+">"+this.FormatXML(Value)+"</"+Name+">");
        this.State="";
    }
    this.Close = function()
    {
        while (this.Nodes.length>0)
            this.EndNode();
        this.State="closed";
    }
    this.ToString = function(){return this.XML.join("");}
}

 

login.js

 

function createXML(){
	try
	{
		XML=new XMLWriter();
		XML.BeginNode("Login");
		XML.Node("Name", document.getElementById("uname").value);
		XML.BeginNode("password");             
		XML.WriteString(document.getElementById("pword").value);
		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.
		
	 alert(XML.ToString().replace(/</g,"\n<"););
	}
	catch(Err)
	{
		alert("Error: " + Err.description);
	}


}

window.οnlοad=function(){
	document.getElementById("aa").οnclick=createXML;
}

 

 

 

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: <MyNode>My Value</MyNode>

XML.BeginNode(“Foo”);
XML.WriteString(“Hello World”);
XML.EndNode();
//Produces <Foo>Hello World</Foo>

Node 方法:
XML.EndNode();
//Produces: <Foo Bar=”Some Value” />

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值