Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-04-2006, 09:28 AM   #1
devolve
Registered User
 
Join Date: Dec 2006
Posts: 4
Default Parsing an XML that uses reserved words.

Hello there. I'm fresh new to ActionScript an have put myself in quite a pickle. I need to parse an XML in a flash to generate some dynamic content, but the problem is that some of the XML-tags are reserved words and wont allow the parser to get through. I need help on a workaround.

I've tried type conversion, as you can see below, and it still does nothing.
I am using XML_Deserializer.as to help me parse this.

Code:
var xml_url = "X.xml";
function parseVideography(success) {
	if (!success) {
		trace("ERROR!");
		return;
	}
	XmlDeserializer.parse_nodes(myData, XmlDeserializer.xml_object.childNodes[0], ['Id']);
	var str_nodes:String = "myData.Lists.List.ListItems.Media";
	var obj_nodes:Object = Object(str_nodes);
	for (var a = 0; a<obj_nodes.length; a++) {
		var nouvArtists:String = XmlDeserializer.get_items(obj_nodes[a].Artist.__content__);
		var nouvUrls:String = XmlDeserializer.get_items(obj_nodes[a]._Id);
		var nouvSongs:String = XmlDeserializer.get_items(obj_nodes[a].Name.__content__);
		var str_IMG:String = "obj_nodes[a].Covers.Front.URL.__content__";
		var obj_IMG:Object = Object(str_IMG);
		var nouvCovers:String = XmlDeserializer.get_items(obj_IMG);
		var song_obj:String = '<a href="VideoPage____7330.aspx?VideoID='+nouvUrls+'"><img width="32" height="32" src="'+nouvCovers+'">'+nouvArtists+'<br>'+nouvSongs+'</a>';
		trace(song_obj);
		nouv.htmlText += song_obj;
	}
}
var myData = {};
XmlDeserializer.debug = true;
XmlDeserializer.parse_xml(xml_url, parseVideography);
XML_Deserializer.as
Code:
/*
Copyright (c) 2005 Rick Olson

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var XmlDeserializer = {};
XmlDeserializer.xml_object = undefined;
XmlDeserializer.debug = false;

// parses given xmlurl, calling onload when it's done loading
//
// example usage:
//
//   <discography>
//     <album title="Pretty Hate Machine">
//       <song number="1">Head Like a Hole</song>
//       <song number="2">Terrible Lie</song>
//     </album>
//     <album title="Broken">
//     </album>
//   </discography>
//
//   // flash xml callback function
//   function parseDiscography(success) {
//     if(!success) return;
//   	
//      // parse xml data into myData object
//   	XmlDeserializer.parse_nodes(myData, XmlDeserializer.xml_object.childNodes[0]);
//
//     converts discography/album into an array, even if there only happens to be one element
//     var albums = XmlDeserializer.get_items(myData.album);
//     for(var a=0; a < albums.length; a++) {
//       trace("Album #" + (a+1) + ': ' + albums[a]._title);
//       var songs = XmlDeserializer.get_items(albums[a].song);
//       for(var s=0; s < songs.length; s++) {
//         trace("song #" + (s+1) + ': ' + songs[s].__content__);
//       }
//     }
//   	play();
//   }
//
//   stop();
//   XmlDeserializer.parse_xml(path_to_xml, parseDiscography);
//
XmlDeserializer.parse_xml = function(xml_url, onload) {
	XmlDeserializer.xml_object = new XML();
	XmlDeserializer.xml_object.ignoreWhite = true;
	XmlDeserializer.xml_object.onLoad = onload;
	XmlDeserializer.xml_object.load(xml_url);
	trace('parsing ' + xml_url);
	trace('debugging: ' + (XmlDeserializer.debug ? 'on' : 'off'));
	stop();
}

// returns node if node is an array, or returns an array of the node
XmlDeserializer.get_items = function(node) {
	if(!node) return [];
	if((typeof(node) == 'object') && (node.length != null))
		return node;
	return [node];
}

/* xml deserializer utility methods */

// parse the xml, baby!
XmlDeserializer.parse_nodes = function(obj, rootNode) {
	var prop;
	if(XmlDeserializer.debug) trace('root has ' + rootNode.childNodes.length + ' children.');

	var attribs = XmlDeserializer.get_attributes(rootNode.toString());
	if(XmlDeserializer.debug) trace('found attributes: ' + attribs.join(', '));
	for(var a=0; a < attribs.length; a++) {
		if(rootNode.attributes[attribs[a]] != null)
			obj['_'+attribs[a]] = rootNode.attributes[attribs[a]];
	}
	
	for(var i=0; i < rootNode.childNodes.length; i++) {
		var node = rootNode.childNodes[i];
		prop = {};

		if(node.firstChild.nodeType == 3) {
			// strip CDATA if it exists
			var v = node.firstChild.nodeValue;
			if(v.slice(0,9) == '<![CDATA[') v = v.slice(9, -3);
			prop.__content__ = v;
		}

		XmlDeserializer.parse_nodes(prop, node);
		XmlDeserializer.set_node_value(obj, node.nodeName, prop);
	}
}

XmlDeserializer.set_node_value = function(obj, key, val) {
	// set it to the new value
	if(obj[key] == null) {
		if(XmlDeserializer.debug) trace('adding new value to ' + key);
		obj[key] = val;
	} else {
		// if array, add value to the array
		if((typeof(obj[key]) == 'object') && (obj[key].length != null)) {
			if(XmlDeserializer.debug) trace('adding array value to ' + key);
			obj[key].push(val);
		// create an array with the current value and the new value)
		} else {
			if(XmlDeserializer.debug) trace('adding value to ' + key);
			obj[key] = [obj[key], val]
		}
	}
}

// totally awesome way of pulling out the attributes from a node
// thanks to Joseph S. Levin
XmlDeserializer.get_data = function(str, first_delimit, second_delimit, action) {
	first_pos   = str.indexOf(first_delimit);
	before      = str.substring(0, first_pos + first_delimit.length);
	after       = str.substring(first_pos + first_delimit.length);
	second_pos  = after.indexOf(second_delimit);
	first_value = after.substring(0, second_pos);
		
	if (action=="parse")
		return (first_value == "") ? -1 : first_value;
	else if (action=="pass")
		return after.substring(second_pos);
}

// totally awesome way of pulling out the attributes from a node
// thanks to Joseph S. Levin
XmlDeserializer.get_attributes = function(str) {
	gt_pos = str.indexOf('>');
	if(gt_pos > -1) str = str.substr(0, gt_pos+1);
	
	var attrs = [], attr_name = '', attr_value = '';
	while (attr_name != -1 && str.length > 0) {
		attr_name = XmlDeserializer.get_data(str, " ", "=\"", "parse");
	
		//check to see if you are at the end of the attribute list, if so continue
		if (attr_name == -1) continue;
		
		attrs.push(attr_name);

		attr_value = XmlDeserializer.get_data(str, "=\"", "\"", "parse");
		
		if(XmlDeserializer.debug) trace("attr: " + attr_name + ' = ' + attr_value);
		
		// revalue str for next pass
		str = XmlDeserializer.get_data(str, "=\"", "\"", "pass");
	} 
	return attrs;
}
devolve is offline   Reply With Quote
Old 12-04-2006, 05:02 PM   #2
mcmcom
Asleep At The Keyboard
 
mcmcom's Avatar
 
Join Date: Jun 2004
Location: Toronto, Canada
Posts: 2,528
Default

did you try wrapping the xml tags in CDATA[ ] elements?

this article may help:

http://www.w3schools.com/xml/xml_cdata.asp

hth,
mcm
__________________
www.mustcodemore.com
Adobe Certified Flash 8 Developer
mcmcom is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Words loaded from xml wont show up (in accordance to slideshow) visionize ActionScript 2.0 0 04-27-2006 09:18 AM
The best way to store a list of words?? Mattiax Other Flash General Questions 4 04-06-2006 04:39 PM
XML Parsing when xml data contains umlaut characters Stephen Cox ActionScript 2.0 2 07-07-2005 08:04 AM
Splitting text into individual words Hutch ActionScript 1.0 (and below) 1 02-24-2005 10:47 PM
php curse words dee19802000 Server-Side Scripting 1 11-23-2003 09:35 PM


All times are GMT. The time now is 08:04 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.