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 人气:309
分享到:
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: alwaysR

  • 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> Th

  • 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="美女"> <name>小乔</name> <s

  • 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

  • [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 version

  • 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

  • asp.net下创建.查询.修改带名称空间的 XML 文件的例子 2014-06-23

    C#: string w3NameSpace = "http://www.w3.org/2000/xmlns/"; System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); //创建根节点 System.Xml.XmlNode root = doc.CreateNode(System.Xml.XmlNodeType.Element, "w", "wordDocument", "http://schemas.microsoft.com/o

  • UNICODE and UTF-8 conversion Xiangjie 2010-03-19

    Reprinted please reference: http://www.ins1000.cn/KnowledgeActionForReader?action=read&id=104 Download the source file: UTF-8 conversion Xiang Jie (Word version) A code in the computer, various information is encoded in binary form, ie, whether text,

  • XML documents using Libxml2 operation 2010-02-25

    1, Libxml2 Description: Libxml2 is an xml parser c-language version, was originally a tool for the Gnome project development, based on the MIT License is a free open source software. It addition to supporting the c-language version, it also supports c + +

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.083 (s). 12 q(s)