佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > JavaScript生成xml

JavaScript生成xml

熱門標簽:銀行業(yè)務(wù) 檢查注冊表項 呼叫中心市場需求 美圖手機 網(wǎng)站文章發(fā)布 服務(wù)器配置 智能手機 鐵路電話系統(tǒng)
復(fù)制代碼 代碼如下:

function XMLWriter()
{
    this.XML=[];
    this.Nodes=[];
    this.State="";
    this.FormatXML = function(Str)
    {
        if (Str)
            return Str.replace(//g, "").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("");}
}



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:
復(fù)制代碼 代碼如下:

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>

標簽:上海 紅河 滄州 河南 沈陽 長治 樂山 新疆

巨人網(wǎng)絡(luò)通訊聲明:本文標題《JavaScript生成xml》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    富民县| 安徽省| 乌拉特中旗| 固原市| 济阳县| 古交市| 佛冈县| 泰顺县| 库车县| 永城市| 缙云县| 安龙县| 榆社县| 景德镇市| 广河县| 鹿泉市| 富顺县| 江华| 交城县| 喀喇沁旗| 迁安市| 海兴县| 探索| 岗巴县| 新平| 开远市| 和政县| 九龙县| 五华县| 丽水市| 呼图壁县| 鲁山县| 邢台县| 天等县| 木里| 平定县| 祁东县| 社会| 额敏县| 天镇县| 大同市|