当前位置: 移动技术网 > IT编程>网页制作>XML > JavaScript生成xml

JavaScript生成xml

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网