1/* 2 ### jQuery XML to JSON Plugin v1.1 - 2008-07-01 ### 3 * Dual licensed under the MIT and GPL licenses: 4 * http://www.opensource.org/licenses/mit-license.php 5 * http://www.gnu.org/licenses/gpl.html 6 ### 7 Website: http://www.fyneworks.com/jquery/xml-to-json/ 8*//* 9 # INSPIRED BY: http://www.terracoder.com/ 10 AND: http://www.thomasfrank.se/xml_to_json.html 11 AND: http://www.kawa.net/works/js/xml/objtree-e.html 12*//* 13 This simple script converts XML (document of code) into a JSON object. It is the combination of 2 14 'xml to json' great parsers (see below) which allows for both 'simple' and 'extended' parsing modes. 15*/ 16// Avoid collisions 17;if(window.jQuery) (function($){ 18 19 // Add function to jQuery namespace 20 $.extend({ 21 22 // converts xml documents and xml text to json object 23 xml2json: function(xml, extended) { 24 if(!xml) return {}; // quick fail 25 26 //### PARSER LIBRARY 27 // Core function 28 function parseXML(node, simple){ 29 if(!node) return null; 30 var txt = '', obj = null, att = null; 31 var nt = node.nodeType, nn = jsVar(node.localName || node.nodeName); 32 var nv = node.text || node.nodeValue || ''; 33 /*DBG*/ //if(window.console) console.log(['x2j',nn,nt,nv.length+' bytes']); 34 if(node.childNodes){ 35 if(node.childNodes.length>0){ 36 /*DBG*/ //if(window.console) console.log(['x2j',nn,'CHILDREN',node.childNodes]); 37 $.each(node.childNodes, function(n,cn){ 38 var cnt = cn.nodeType, cnn = jsVar(cn.localName || cn.nodeName); 39 var cnv = cn.text || cn.nodeValue || ''; 40 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>a',cnn,cnt,cnv]); 41 if(cnt == 8){ 42 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>b',cnn,'COMMENT (ignore)']); 43 return; // ignore comment node 44 } 45 else if(cnt == 3 || cnt == 4 || !cnn){ 46 // ignore white-space in between tags 47 if(cnv.match(/^\s+$/)){ 48 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>c',cnn,'WHITE-SPACE (ignore)']); 49 return; 50 }; 51 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>d',cnn,'TEXT']); 52 txt += cnv.replace(/^\s+/,'').replace(/\s+$/,''); 53 // make sure we ditch trailing spaces from markup 54 } 55 else{ 56 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>e',cnn,'OBJECT']); 57 obj = obj || {}; 58 if(obj[cnn]){ 59 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>f',cnn,'ARRAY']); 60 61 // http://forum.jquery.com/topic/jquery-jquery-xml2json-problems-when-siblings-of-the-same-tagname-only-have-a-textnode-as-a-child 62 if(!obj[cnn].length) obj[cnn] = myArr(obj[cnn]); 63 obj[cnn] = myArr(obj[cnn]); 64 65 obj[cnn][ obj[cnn].length ] = parseXML(cn, true/* simple */); 66 obj[cnn].length = obj[cnn].length; 67 } 68 else{ 69 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>g',cnn,'dig deeper...']); 70 obj[cnn] = parseXML(cn); 71 }; 72 }; 73 }); 74 };//node.childNodes.length>0 75 };//node.childNodes 76 if(node.attributes){ 77 if(node.attributes.length>0){ 78 /*DBG*/ //if(window.console) console.log(['x2j',nn,'ATTRIBUTES',node.attributes]) 79 att = {}; obj = obj || {}; 80 $.each(node.attributes, function(a,at){ 81 var atn = jsVar(at.name), atv = at.value; 82 att[atn] = atv; 83 if(obj[atn]){ 84 /*DBG*/ //if(window.console) console.log(['x2j',nn,'attr>',atn,'ARRAY']); 85 86 // http://forum.jquery.com/topic/jquery-jquery-xml2json-problems-when-siblings-of-the-same-tagname-only-have-a-textnode-as-a-child 87 //if(!obj[atn].length) obj[atn] = myArr(obj[atn]);//[ obj[ atn ] ]; 88 obj[cnn] = myArr(obj[cnn]); 89 90 obj[atn][ obj[atn].length ] = atv; 91 obj[atn].length = obj[atn].length; 92 } 93 else{ 94 /*DBG*/ //if(window.console) console.log(['x2j',nn,'attr>',atn,'TEXT']); 95 obj[atn] = atv; 96 }; 97 }); 98 //obj['attributes'] = att; 99 };//node.attributes.length>0 100 };//node.attributes 101 if(obj){ 102 obj = $.extend( (txt!='' ? new String(txt) : {}),/* {text:txt},*/ obj || {}/*, att || {}*/); 103 txt = (obj.text) ? (typeof(obj.text)=='object' ? obj.text : [obj.text || '']).c
103oncat([txt]) : txt; 104 if(txt) obj.text = txt; 105 txt = ''; 106 }; 107 var out = obj || txt; 108 //console.log([extended, simple, out]); 109 if(extended){ 110 if(txt) out = {};//new String(out); 111 txt = out.text || txt || ''; 112 if(txt) out.text = txt; 113 if(!simple) out = myArr(out); 114 }; 115 return out; 116 };// parseXML 117 // Core Function End 118 // Utility functions 119 var jsVar = function(s){ return String(s || '').replace(/-/g,"_"); }; 120 121 // NEW isNum function: 01/09/2010 122 // Thanks to Emile Grau, GigaTecnologies S.L., www.gigatransfer.com, www.mygigamail.com 123 function isNum(s){ 124 // based on utility function isNum from xml2json plugin (http://www.fyneworks.com/ - [email protected]) 125 // few bugs corrected from original function : 126 // - syntax error : regexp.test(string) instead of string.test(reg) 127 // - regexp modified to accept comma as decimal mark (latin syntax : 25,24 ) 128 // - regexp modified to reject if no number before decimal mark : ".7" is not accepted 129 // - string is "trimmed", allowing to accept space at the beginning and end of string 130 var regexp=/^((-)?([0-9]+)(([\.\,]{0,1})([0-9]+))?$)/ 131 return (typeof s == "number") || regexp.test(String((s && typeof s == "string") ? jQuery.trim(s) : '')); 132 }; 133 // OLD isNum function: (for reference only) 134 //var isNum = function(s){ return (typeof s == "number") || String((s && typeof s == "string") ? s : '').test(/^((-)?([0-9]*)((\.{0,1})([0-9]+))?$)/); }; 135 136 var myArr = function(o){ 137 138 // http://forum.jquery.com/topic/jquery-jquery-xml2json-problems-when-siblings-of-the-same-tagname-only-have-a-textnode-as-a-child 139 //if(!o.length) o = [ o ]; o.length=o.length; 140 if(!Array.isArray(o)) o = [ o ]; o.length=o.length; 141 142 // here is where you can attach additional functionality, such as searching and sorting... 143 return o; 144 }; 145 // Utility functions End 146 //### PARSER LIBRARY END 147 148 // Convert plain text to xml 149 if(typeof xml=='string') xml = $.text2xml(xml); 150 151 // Quick fail if not xml (or if this is a node) 152 if(!xml.nodeType) return; 153 if(xml.nodeType == 3 || xml.nodeType == 4) return xml.nodeValue; 154 155 // Find xml root node 156 var root = (xml.nodeType == 9) ? xml.documentElement : xml; 157 158 // Convert xml to json 159 var out = parseXML(root, true /* simple */); 160 161 // Clean-up memory 162 xml = null; root = null; 163 164 // Send output 165 return out; 166 }, 167 168 // Convert text to XML DOM 169 text2xml: function(str) { 170 // NOTE: I'd like to use jQuery for this, but jQuery makes all tags uppercase 171 //return $(xml)[0]; 172 var out; 173 try{ 174 var xml = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser(); 175 xml.async = false; 176 }catch(e){ throw new Error("XML Parser could not be instantiated") }; 177 try{ 178 if($.browser.msie) out = (xml.loadXML(str))?xml:false; 179 else out = xml.parseFromString(str, "text/xml"); 180 }catch(e){ throw new Error("Error parsing XML string") }; 181 return out; 182 } 183 184 }); // extend $ 185 186})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.