Uncaught Typeerror: Cannot Read Property 'concat' of Undefined

Convert XML to JSON with JavaScript

If y'all follow me on Twitter, you know that I've been working on a super top undercover mobile application using Appcelerator Titanium.  The experience has been great:  using JavaScript to create easy to write, easy to exam, native mobile apps has been fun.  My mobile app connects to numerous social network APIs, some of which but provide an XML response.  My mini "framework" uses JSON to dynamically create widgets so I've needed a way to turn XML into JSON.  I institute many solutions but none of them worked.  Afterward tweaking an existing function, I've institute a solution that works bang-up.

The JavaScript

It's important to signal out that Titanium'south Titanium.XML.DOMDocument object implements DOM2-level structures.  Here'due south the magic XML to JSON lawmaking:

// Changes XML to JSON function xmlToJson(xml) { 	 	// Create the return object 	var obj = {};  	if (xml.nodeType == i) { // element 		// do attributes 		if (xml.attributes.length > 0) { 		obj["@attributes"] = {}; 			for (var j = 0; j < xml.attributes.length; j++) { 				var attribute = xml.attributes.detail(j); 				obj["@attributes"][attribute.nodeName] = attribute.nodeValue; 			} 		} 	} else if (xml.nodeType == iii) { // text 		obj = xml.nodeValue; 	}  	// do children 	if (xml.hasChildNodes()) { 		for(var i = 0; i < xml.childNodes.length; i++) { 			var item = xml.childNodes.item(i); 			var nodeName = item.nodeName; 			if (typeof(obj[nodeName]) == "undefined") { 				obj[nodeName] = xmlToJson(item); 			} else { 				if (typeof(obj[nodeName].push) == "undefined") { 					var one-time = obj[nodeName]; 					obj[nodeName] = []; 					obj[nodeName].button(old); 				} 				obj[nodeName].push(xmlToJson(particular)); 			} 		} 	} 	render obj; };        

The major modify I needed to implement was using attributes.item(j) instead of the attributes[j] that most of the scripts I institute used.  With this function, XML that looks like:

<ALEXA VER="0.9" URL="davidwalsh.proper noun/" Abode="0" AID="="> 	<SD TITLE="A" FLAGS="" HOST="davidwalsh.proper name"> 		<TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/> 		<LINKSIN NUM="1102"/> 		<SPEED TEXT="1421" PCT="51"/> 	</SD> 	<SD> 		<POPULARITY URL="davidwalsh.name/" TEXT="7131"/> 		<REACH RANK="5952"/> 		<RANK DELTA="-1648"/> 	</SD> </ALEXA>        

...becomes workable a JavaScript object with the following structure:

{ 	"@attributes": { 		Assistance: "=", 		Dwelling house:  0, 		URL: "davidwalsh.name/", 		VER: "0.ix", 	}, 	SD = [ 		{ 			"@attributes": { 				FLAGS: "", 				HOST: "davidwalsh.proper noun", 				TITLE: A 			}, 			LINKSIN: { 				"@attributes": { 					NUM: 1102 				} 			}, 			SPEED: { 				"@attributes": { 					Percentage: 51, 					TEXT: 1421 				} 			}, 			Title: { 				"@attributes": { 					TEXT: "David Walsh Weblog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else", 				} 			}, 		}, 		{ 			POPULARITY: { 				"@attributes": { 					TEXT: 7131, 					URL: "davidwalsh.name/" 				} 			}, 			RANK: { 				"@attributes": { 					DELTA: "-1648" 				} 			}, 			Accomplish: { 				"@attributes": { 					RANK = 5952 				} 			} 		} 	] }        

From here you tin use the JavaScript object however yous see fit. If you'd like the JavaScript in string JSON format, y'all tin code:

// Assuming xmlDoc is the XML DOM Document var jsonText = JSON.stringify(xmlToJson(xmlDoc));        

This function has been extremely useful in allowing me to rapidly condone XML and utilize JSON instead.  The role works well when structuring attributes and arrays of nested child nodes.  Continue this handy;  at some point yous may need to convert XML to JSON!

Website performance monitoring

Website performance monitoring

Recent Features

  • From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!
  • Create Namespaced Classes with MooTools

    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes similar the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, simply each to their own.  In any effect...

hamiltontiand1952.blogspot.com

Source: https://davidwalsh.name/convert-xml-json

0 Response to "Uncaught Typeerror: Cannot Read Property 'concat' of Undefined"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel