Using XML in NeoSwiff

| 5 Comments | No TrackBacks

Someone had asked me how to load, parse, and display .xml files using NeoSwiff. I created a simple example you can download here that does just that.

This example runs in the standalone version of NeoSwiff. It consists of two classes - the main application form, and a "Track" class that represents data in the xml file. The final result looks like this:

The code should be pretty straight forward. You create an XmlRequest that is capable of loading an external .xml file and listen to it's StatusChanged event. When the Status changes, you examine it, and if it is equal to XmlRequestStatus.Success then the file was loaded successfully and you can start reading the data from the Response. If there was an error, you can examine the request's XmlError property and check for things like XmlError.CommentNotProperlyTerminated, XmlError.MalformedXmlElement, etc.

The parse function I created looks at the XmlRequest's Response property (which is of type XmlDocument) and essentially just does the FirstChild / ChildNodes loop that you're probably familiar with in parsing XML in Flash. Note in C# we're using the "foreach" keyword to loop over both ChildNodes and Attributes. A quick code example:

/** Loops over the XML document and populates the list box */
private void ParseXml(XmlDocument xmlDoc) {
	XmlNode playlist = xmlDoc.FirstChild;
	
	foreach (XmlNode track in playlist.ChildNodes) {
		list.Items.Add( ParseTrack(track) );
	}
}
	
/** Reads individual track information */
private Track ParseTrack(XmlNode trackNode) {
	Track track = new Track();
			
	foreach (XmlAttribute a in trackNode.Attributes) {
		if (a.Name == "mp3") {
		   track.Url = a.Value;
		}
	}

	// the first child of the track node is the artist node.. 
	// and the first child of that is the CDATA text, so get the value
	// which is the artist name
	track.Artist = trackNode.FirstChild.FirstChild.Value;
			
	// the second child of the track node is the title node...
	track.Title = trackNode.FirstChild.NextSibling.FirstChild.Value;
			
	return track;
}

Also, the Track class looks like this:

/** 
 * A class that represents a single track entity in the XML file - notice
 * we implement IListItem with a DisplayText getter so the ListBox knows what
 * to display when a Track is added to it's Items.
 */
class Track : IListItem {
	
	public string Url;
	public string Artist;
	public string Title;
		
	public Track() : this("", "", "") {}	
	public Track(string url, string artist, string title) {
		this.Url = url;
		this.Artist = artist;
		this.Title = title;
	}
		
	public string DisplayText {
		get {
			return Artist;
		}
	}
}

This example is pretty simple, but shows you how you can load, check the status of, parse, and use xml data in your .swf files created via NeoSwiff. Essentially, this is exactly how I would do it in ActionScript, though everything here is typed just a bit differently. I had posted similar code in ActionScript awhile ago, for comparison purposes. Notice that the ActionScript code is a lot shorter (easier?), but the C# code feels a little more structured.

No TrackBacks

TrackBack URL: http://www.darronschall.com/mt/mt-tb.cgi/46

5 Comments

I'm very impressed with NeoSwiff up 'till now. Heck, even the IDE is decent. But I'm afraid of the price at launch...

Well, Xamlon Pro announced pricing at $500 so I would expect NeoSwiff to be in that price range, but I have no idea what GlobFX is planning. We'll have to just wait and see...

I must be slow, but do I need to use the standalone IDE for everything right now? I installed the Visual Studio plugin and I can open existing projects but can't create new ones from the "new blank solution" option and choose a neoswiff project...

I just found that a key thing to getting the XmlRequest to work with relative paths is to have the PARAM with the BASE path set to the proper directory. Is this something that only applies SWFs that are created with NeoSwiff or does it apply to all Flash objects?

- grant - I tried to use the VS.NET plugin too and it did exactly what you said ... useless. I also emailed GlobFX about the issue and heard nothing back.

I speak to them very often (each week) and can tell you that the price would be between 1000-1500 dollar.

I'm sorry Xamlon is cheaper but not like Neoswiff.

Leave a comment



About this Entry

This page contains a single entry by darron published on May 17, 2005 8:25 AM.

Phishers need to proofread too... was the previous entry in this blog.

How to use Flex without a server is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Archives

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02