Json数据如果作为配置文件比较难读懂,XML文件作为配置文件有先天的优势,容易读懂和配置,因此不考虑效率时,在页面中宁可用XML文件作为配置文件再用JS做一次转化把XML转成JSON使用

比如如下相对比较复杂的XML

<myobjects>

  <!--object 1-->
  <myobject>
    <id>yourID_1</id>
    <name>your name</name>
    <description>
      <![CDATA[Merck Biologics Pilot Plant  ]]>
    </description>
    <locations>
      <location>
        <name><![CDATA[your location]]></name>
        <description>
          <![CDATA[<div id="div_desc1">your description, can be with html tags</div>]]>
        </description>
        <link>http://www.google.co.uk</link>
        <top>65</top>
        <left>501</left>
        <content_top>-20</content_top>
        <content_left>0</content_left>
      </location>
    </locations>
  </myobject>

  <!--object 2-->
  <myobject>
    <id>yourID_2</id>
    <name>your name</name>
    <description><![CDATA[your description, can be with html tags]]></description>
    <locations>
      <location>
        <name><![CDATA[your location]]></name>
        <description>
          <![CDATA[<div id="div_desc2">your description, can be with html tags</div>       
        ]]>
        </description>
        <link>http://www.google.co.uk</link>
        <top>65</top>
        <left>350</left>
        <content_top>-20</content_top>
        <content_left>0</content_left>
      </location>
    </locations>
  </myobject>

</myobjects>

如果你要用时可能要读取连带Html内容片段一起使用,那么转Json 的Jquery扩展插件代码如下:

(function ($) {
    //Converts XML DOM to JSON
    $.extend({
        xmlToJSON: function (xdoc) {
            try {
                if (!xdoc) { return null; }
                var tmpObj = {};
                tmpObj.typeOf = "JSXBObject";
                var xroot = (xdoc.nodeType == 9) ? xdoc.documentElement : xdoc;
                tmpObj.RootName = xroot.nodeName || "";
                if (xdoc.nodeType == 3 || xdoc.nodeType == 4) {//Text Value or CDATA
                    return xdoc.nodeValue;
                }

                //Alters attribute and collection names to comply with JS
                function formatName(name) {
                    var regEx = /-/g;
                    var tName = String(name).replace(regEx, "_");
                    return tName;
                }
                //Set Attributes of an object
                function setAttributes(obj, node) {
                    if (node.attributes.length > 0) {
                        var a = node.attributes.length - 1;
                        var attName;
                        obj._attributes = [];
                        do { //Order is irrelevant (speed-up)
                            attName = String(formatName(node.attributes[a].name));
                            obj._attributes.push(attName);                  //key
                            obj[attName] = $.trim(node.attributes[a].value);//value
                        } while (a--);
                    }
                }

                //Recursive JSON Assembler
                //Set Object Nodes
                function setObjects(obj, node) {
                    var elemName;    //Element name
                    var cnode;    //Current Node
                    var tObj;    //New subnode
                    var cName = "";
                    if (!node) { return null; }
                    //Set node attributes if any
                    if (node.attributes.length > 0) { setAttributes(obj, node); }
                    obj.Text = "";
                    if (node.hasChildNodes()) {
                        var nodeCount = node.childNodes.length - 1;
                        var n = 0;
                        do { //Order is irrelevant (speed-up)
                            cnode = node.childNodes[n];
                            switch (cnode.nodeType) {
                                case 1: //Node
                                    //Process child nodes
                                    obj._children = [];
                                    //SOAP XML FIX to remove namespaces (i.e. soapenv:)
                                    elemName = (cnode.localName) ? cnode.localName : cnode.baseName;
                                    elemName = formatName(elemName);
                                    if (cName != elemName) { obj._children.push(elemName); }
                                    //Create sub elemns array
                                    if (!obj[elemName]) {
                                        obj[elemName] = []; //Create Collection
                                    }
                                    tObj = {};
                                    obj[elemName].push(tObj);
                                    if (cnode.attributes.length > 0) {
                                        setAttributes(tObj, cnode);
                                    }

                                    cName = elemName;
                                    if (cnode.hasChildNodes()) {
                                        setObjects(tObj, cnode); //Recursive Call
                                    }
                                    break;
                                case 3: //Text Value
                                    obj.Text += $.trim(cnode.nodeValue);
                                    break;
                                case 4: //CDATA
                                    obj.Text += (cnode.text) ? $.trim(cnode.text) : $.trim(cnode.nodeValue);
                                    break;
                            }
                        } while (n++ < nodeCount);
                    }
                }
                //START TO RUN
                setObjects(tmpObj, xroot);
                //Clean-up memmory
                xdoc = null;
                xroot = null;
                return tmpObj;

            } catch (e) {
                return null;
            }
        }
    });

})(jQuery);

其实实现原理比较简单,处理XML与JSON数据,比较通用的思路就是递归调用,设置好递归出口条件,然后循环读取节点和设置节点,读取XML每个节点的同时,生成JSON相应的每个节点。

Json数据如果作为配置文件比较难读懂,XML文件作为配置文件有先天的优势,容易读懂和配置,因此不考虑效率时,在页面中宁可用XML文件作为配置文件再用JS做一次转化把XML转成JSON使用,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。