z4x, jquery xml parsing plugin, with oh well

z4x, had intended to write separately, but it is a lot of users jquery, take the ride to take it and transform into a jquery xml parsing plug, used with colleagues say oh well. By the way with a reflection method.

The plug has two methods:

1). Z4x xml parsing and object into json.

2). Reflection method can be reflected json object.

0. Jquery.z4x.js Source:

$.extend({
    z4x: function(s) {
        var dom;
        if (typeof(s) == "object") {
            dom = s;
        } else if (typeof(s) == "string") {
            if (window.ActiveXObject) {
                dom = new ActiveXObject("Microsoft.XmlDom");
                dom.async = "false";
                dom.loadXML(s);
            } else {
                dom = new DOMParser().parseFromString(s, "text/xml");
            }
        }
        var _dig = function(ele) {
            var oo = {};
            var alen = (ele.attributes) ? ele.attributes.length: 0;
            for (var i = 0; i < alen; i++) {
                oo["$" + ele.attributes[i].name] = ele.attributes[i].value;
            }

            var elen = ele.childNodes.length;
            if (elen == 0) return oo;

            var tem;
            for (var i = 0; i < elen; i++) {
                tem = oo[ele.childNodes[i].nodeName];

                if (typeof(tem) == "undefined") {

                    if (ele.childNodes[i].childNodes.length == 0) {

                        if (ele.childNodes[i].nodeName == "#text" || ele.childNodes[i].nodeName == "#cdata-section") {
                            oo["$$"] = [ele.childNodes[i].nodeValue];
                        } else {
                            oo[ele.childNodes[i].nodeName] = [_dig(ele.childNodes[i])];
                        }

                    } else {
                        oo[ele.childNodes[i].nodeName] = [_dig(ele.childNodes[i])];
                    }
                } else {
                    tem[tem.length] = _dig(ele.childNodes[i]);
                    oo[ele.childNodes[i].nodeName] = tem;
                }
            }
            return oo;
        };

        var oo = {};
        oo[dom.documentElement.nodeName] = _dig(dom.documentElement);
        return oo;
    },
    ref : function(o,sp)
    {
        sp = sp?sp:"\n";
        var tem = [];
        for(var i in o) tem[tem.length]=i+":"+o[i];
        return tem.join(sp);
    }
});

Eg:

1. A.xml

<?xml version="1.0" encoding="utf-8"?>
<root>
    <a desc="just test">abc</a>
    <b><![CDATA[
        <c>test</c>
    ]]></b>
</root>

2. Call

$(function(){
    $.get("a.xml",function(data){
        var json = $.z4x(data);// In just one step, becomes  json.
        alert($.ref(json));// Reflex  json

        //alert($.ref(json.root));// Continue to reflection, and see what it  
  });
})


Ah is not very simple, fast try.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of z4x, jquery xml parsing plugin, with oh well

  • Process migration from tomcat to websphere changes

    Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm's

  • JAVA EE JSP_JNDI

    dsfdsa http://lindows.javaeye.com/admin/blogs/213348 Tomcat 6 with the connection pool data source configuration http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html project: test Driver path: D: \ workspace \ test \ WebRoot \ WEB-INF \ lib ...

  • Hibernate.cfg.xml configuration file (including the primary key generation strategy Introduction)

    Hibernate.cfg.xml configuration file: <? xml version = "1.0" encoding = "utf-8"?> <! DOCTYPE hibernate-configuration PUBLIC "- / / Hibernate / Hibernate Configuration DTD / / EN" "hibernate-configuration-2.0.dtd

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries