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.

分类:Web 时间:2010-04-17 人气:311
分享到:
blog comments powered by Disqus

相关文章

  • AndroiManifest.xml android file attributes 2011-05-16

    android: allowTaskReparenting Whether to allow the replacement activity dependent tasks, such as short message from a task switch to the browser task. -------------------------------------------------- ------------------------------------ android: al

  • javascript methods and techniques Daquan 2 (transfer) 2008-09-07

    166.xml data island bound form <html> <body> <xml src="test.xml"> </ xml> <table border='1' datasrc='#abc'> <thead> <td> receiver </ td> <td> sender </ td> <td> Theme </ td>

  • XML CDATA是什么? 2015-01-18

    这篇文章主要为大家介绍下XML CDATA是什么,学习xml的朋友可以参考下 All text in an XML document will be parsed by the parser. XML文档中的所有文本都会被解析器解析. Only text inside a CDATA section will be ignored by the parser. 只有CDATA部分的文本会被解析器忽略 Parsed Data 被解析的数据 XML parsers normally parse al

  • php操作xml入门之cdata区段 2015-03-03

    这篇文章主要介绍了php操作xml入门之cdata区段,分析了CDATA的功能及用法,需要的朋友可以参考下 本文实例讲述了php操作xml入门之cdata区段.分享给大家供大家参考.具体分析如下: <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <class> <student id="No<001" haha="美

  • PHP XML parsing class 2010-03-12

    <?php /* +----------------------------------------------------------------------+ | SofeeFramework for PHP 4 | +----------------------------------------------------------------------+ | Copyright (c) 2004-2005 Sofee Development Team.(http://www.sofee

  • As long as you dare come in, no school will not drop xml 2010-03-29

    1 Introduction The vast majority of resources are carried over posted http://www.xml.org.cn/ Statement first, so it was said that even Daolian SHOW TIME 2, gold and equipment XML Explorer English the official version of (free) XML.ORG.CN download (re

  • Two analytical models XML 2010-08-10

    1. Overview Java language and XML data formats are to be considered in the past few decades one of the most important IT technology, Java as the programming provides a platform-independent language, and XML data exchange was to provide a platform-ind

  • [Transfer] AS3 XML in the new approach - E4X 2010-10-15

    Today we take a look at the new AS3 XML approach: E4X, until now, ECMA scripting language specification (ECMA-262) - AscriptScript 3.0 core foundation, and did not provide any of the XML data processing class or method. AcriontScript the previous ver

  • The new AS3 XML processing - E4X 2010-11-11

    Today we take a look at the new AS3 XML approach: E4X, until now, ECMA scripting language specification (ECMA-262) - AscriptScript 3.0 core foundation, and did not provide any of the XML data processing class or method. AcriontScript the previous ver

  • Flex in the operation of XML (the) 2010-11-30

    Introducing the Flex in the operation of a XML, first introduced to XML in simple basic terms. Element: XML in a start tag and end tag of this piece called "Elements" Nodes: the combination of XML elements and text nodes collectively referred to

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.195 (s). 13 q(s)