JavaScript生成xml
2023-06-13 09:13:44 时间
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>
相关文章
- java解析xml element_java解析XML Node与Element的区别(推荐)「建议收藏」
- java dom4j生成xml格式化_Java DOM4J方式生成XML的方法「建议收藏」
- JavaScript笔记(19)之JS执行机制
- javascript html转换成markdown,如何使用Turndown使用JavaScript将HTML转换为Markdown[通俗易懂]
- JavaScript日期处理不再难!Day.js带你飞!
- JavaScript转换农历类详解编程语言
- 典型的JavaScript面试题请收藏详解编程语言
- 深入理解Oracle数据库XML编程(oracle数据库xml)
- [代码]比较XML文件差异[cl_proxy_ui_utils=>show_xml_diff]详解编程语言
- 轻松玩转Linux:修改XML文件的正确打开方式(linux修改xml文件)
- MySQL高效操作XML数据(mysql操作xml)
- Oracle 数据库如何高效保存和管理XML数据?(oracle保存xml)
- Importing XML files into MySQL: The Complete Guide for Data Integration(xml文件导入mysql)
- MSSQL方式快速生成XML文件(mssql 生成xml)
- 使用MySQL中XML实现条件判断(mysql中xml判断)
- MySQL与XML的完整解读(mysql xml详解)
- MySQL数据库如何使用XML文件进行数据存储与交换(mysql xml文件)
- 深入剖析MySQL与XML数据交互中的符号使用方法(mysql xml $和)
- 数字金额千位分隔的Javascript
- Javascript里使用Dom操作Xml
- 用函数式编程技术编写优美的JavaScript
- javascript下有关dom以及xml节点访问兼容问题
- 拥抱模块化的JavaScript
- 浅谈Javascript鼠标和滚轮事件
- javascript读写XML实现广告轮换(兼容IE、FF)
- JavaScript中的对象序列化介绍