JavaScript生成xml 转自soar的专栏
function XMLWriter()
{
this.XML=[];
this.Nodes=[];
this.State="";
this.FormatXML = function(Str)
{
if (Str)
return Str.replace(/&/g, "&").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("<"+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("");}
}
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” />
eg:
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(/</g,"\n<");
}
catch(Err)
{
alert("Error: " + Err.description);
}
return false;
}
生成的xml为:
<Example SomeAttribute="And Some Value" AnotherAttrib="...">This is an example of the JS XML WriteString method.
<Name>Value
</Name>
<SubNode>
<SubNode2/>
<SubNode3>Blah blah.
</SubNode3>
</SubNode>
</Example>
分享到:
相关推荐
AJAX 指异步 JavaScript 及 XML(Asynchronous JavaScript And XML)。 AJAX 是一种在 2005 年由 Google 推广开来的编程模式。 AJAX 不是一种新的编程语言,而是一种使用现有标准的新方法。 通过 AJAX,你可以创建更...
javascript操作xml生成树形菜单 javascript操作xml生成树形菜单 javascript操作xml生成树形菜单
AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。
MSXML与JavaScript语言技术进行XML编程 ,讲述JavaScript生成DOM文档对象的方法
在前台页面调用javascript,读写XML文件
您将探索如何使用Ajax来增强站点并使其具有Web 2.0的感觉,以及其他JavaScript增强如何将Web浏览器和网站变成真正的应用程序。
JavaScript读取XML节点生成多级菜单
使用JavaScript+XML+XSL创建树形目录,源码及示例
w3c 生成xml 文件w3c 生成xml 文件w3c 生成xml 文件
强大的统计图表生成,数据源支持xml,excel非常好用
AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。 主要包含了以下几种技术 Ajax的定义 基于web标准XHTML+CSS的表示; 使用 DOM进行动态显示及...
Javascript部分根据网上流传的XML树修改而得 软件功能: 1,可以根据系统文件夹结构动态生成XML,利用DOS tree命令,很高效 2,根据XML动态生成Javascript树,也就是把windows文件夹结构显示到web页面 ...
本程序目录名字必须为MUSIC,因为后台生成页面及XML 需要。 后台登陆: /music_admin/index.asp 数据库: /music_admin/@_#_%_&_a.asa 登陆用户及密码: admin (可自行上数据库修改) 安装方法 把整个目录随意...
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") 'JAVASCRIPT中的创建对象 set objXml=CreateObject("Microsoft.XMLDOM") '创建解析器对象 objXml.async="false" objXml.load("test.xml") '装载xml文件 ...
文中尽量用上所有的操作和属性,以做示范 代码如下:[removed] <... //创建文件头 var p = doc.createProcessingInstruction(“xml”,”version=’1.0′ encoding=’gb2312′”); //添加文件头 do
前些天在做项目时遇到一个很大的困难,就是在javascript中动态创建一个链接,链接到服务器上的某个附件,点击它就可以查看或是下载。代码我都已经写好如下: function createHref(pathname){ var newHref=document....
主要介绍了JavaScript实现将xml转换成html table表格的方法,实例分析了javascript操作XML文件与table表格的技巧,非常具有实用价值,需要的朋友可以参考下
免费的JavaScript编辑器是用于专业编辑JavaScript代码和创建动画和其他特殊效果网页使用DOM中,使用DOM, DHTML, CSS, Ajax(异步JavaScript和XML)和JavaScript 。 AJAX技术开发人员可以很容易地使用该程序的先进的...
XMLWriter 关于XMLWriter XMLWriter是基于.NET的XMLTextWriter的Javascript XML生成器。 原始版本已发布在。 它最初在github上发布。
xml导出多级导航栏.通过javascript读取xml然后生成print页面中的导航栏