Seeking (free?) applications that can sort XML data by a node of your choice?
August 18, 2004 9:42 AM   Subscribe

XML: I'm writing a Flash/Actionscript program that reads in an XML data file. The info needs to be sorted alphabetically, but people will be continually adding new data to the end of the file, so it will have to be resorted often. There's no easy way to sort XML data in Actionscript (and doing so would slow down the program too much, anway), so the data-entry folks will need to sort the data externally to the Flash app. Are there any (free?) applications that can sort XML data by a node of your choice? I need a solution that can run on a Mac or a PC -- or it could be web based.
posted by grumblebee to Technology (16 answers total)
Just use XSL to sort the file.
posted by riffola at 9:50 AM on August 18, 2004

Are you sure it would slow down the program too much?

If you only load the data once, and you are presumably loading it into an array, there actually is a pretty easy way to do it (Array.sort). You're looking at like 5 lines of actionscript. Performance is obviously dependent on the size of the array.

If that's still no good, the web based route is the way to go -- it would take like 15 minutes to crank out a little web based app with a file upload field that takes an XML file, resorts it on a hardcoded field, and then saves it out for download.

I'd be surprised if there was one already built that does exactly what you need though.
posted by malphigian at 9:52 AM on August 18, 2004

Response by poster: I'll look into the web-based idea. Anyone know if there's already something like this in, say, php.

Malphigian, the sorting itself wouldn't take that many lines of code, but don't seem efficient. This will be about 400 nodes to start with, and new nodes will be added about once a week. But they always will need to be displayed in alphabetical order. It seems silly to make the end-user wait even 2 seconds while Actionscript sorts the list, since the list will only need to be sorted once a week.

I'm tempted to tell the data-entry people to just sort "by hand" by sticking the new nodes in between the right old nodes, but I'd like to make things simple for them, too. I'd like to tell them to just stick the new info on the end and then to run the data through some sort of magic xml-sort program.

I used to do a lot of php coding, but I haven't done much server-side stuff in the last few years. I can regain my skills if necessary, but if anyone has a ready-made php function, I'd be grateful.
posted by grumblebee at 10:00 AM on August 18, 2004

You are correct in that the solution to this problem lies in having the data in the xml document pre-sorted before you render it in your Flash thingy. It is inefficient to sort it into the same order every time it is rendered for display.

Is the xml file really produced by someone hand-editing it? If so, then there should be some sort of post edit processing done on the file to sort it and re-output it. This is already described above as a web based application. Some sort of command line utility should also be reasonably easy to develop.

If the xml file is produced as output from some other data entry system, maybe a change can be made there to sort the data before outputting it as xml.
posted by rglasmann at 10:17 AM on August 18, 2004

Telling data-entry people to sort a 400+ node xml file "by hand" is a recipe for disaster.

Just sort an array in flash - it'll take a few minutes to code and a second or two max to execute on the client.

(If you're going to do any server-side code because you want to save the user a few seconds, you might as well use it to serve a chunk of xml at a time too, so the whole file doesn't have to be loaded at once.)
posted by cell at 10:22 AM on August 18, 2004

Response by poster: I just read on a usenet posting that some guy developed a Flash/Actionscript function that sorted XML data. It took 5 seconds to sort 300 items. Maybe his code wasn't well optimized, but that's WAY too long.

I agree with rglasmann that the sorting should be done outside of Flash.

I guess I am still holding out for a finished solution. I'm on a tight deadline, and I'd rather not break out the PHP manuals (I've never used PHP to manipulate XML, and I've never used XSL.) I'll do that if I have too, but only as a last resort.

The xml data will be created completely by hand-coding. I realize that this could lead to all sorts of problems. It's out of my control.
posted by grumblebee at 10:28 AM on August 18, 2004

Why don't you use xml as a translation between a mySQL database and flash? You could have mySQL spit out an already sorted xml file every n hours that flash can read. I think there is also a way to use actionscript query a mySQL database to directly spit out a XML file. That way you could have the data in a format that can be easier to access. Then you could write up a bit of php or flash that you could use to enter data to the mySQL database (passord protect the method, though). I don't know if a database would be overkill for your use.
posted by plemeljr at 10:38 AM on August 18, 2004

Response by poster: Nice idea, plemeljr, but I don't think I'm making my situation clear:

The data will be in the form of a hand-coded xml file. I don't like that idea. I would rather use a database, but I can't control that aspect of the project.

But I would LIKE to offer the data-entry people a quick way to put the XML file in alphabetical order.

I have total control over the client side (Flash). I can upload a server script. I can suggest data-entry techniques. But I can't replace the xml file with a database.
posted by grumblebee at 11:01 AM on August 18, 2004

This sort of thing is useful for me to know too, so I just make a quick script to generate a 1000-node xml object, then pull its nodes into an array and sort on an attribute (50 random characters from A-Z).

The generation of the object obviously takes a while, but the sorting only takes about 400ms on my 2-year-old laptop.

(paste this into a blank movie if you want to see)
posted by cell at 11:20 AM on August 18, 2004

Link to the relevent php manual page here. Take a look at the user comments below for lots of examples of how to do various stuff. You could probably cut and paste a lot of the code.

I'd second not doing the actual sorting in flash. Maybe, if the design allows it, load chunks of xml seperately. Flash doesn't like receiving (or sending) lots of xml at a time.

(spent all day making changes to a gambling game in flash that recieves bet information via xml--gah)
posted by toby\flat2 at 11:21 AM on August 18, 2004

(the number of hacks in this thread is pretty funny...)

Riffola's right, use XSL. It's what it's made for. In the following code it's the xsl:sort that does it, so google for more on that term if you want.

Source data:

XSLT (which sorts by <artist>)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">

<xsl:template match="/">
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<artist><xsl:value-of select="artist"/></artist>
<title><xsl:value-of select="title"/></title>


posted by holloway at 3:07 PM on August 18, 2004

Response by poster: Awesome, holloway (and Riffola). Thanks!

So what's the quickest resource for learning XSL as implemented in PHP?
posted by grumblebee at 3:28 PM on August 18, 2004

Well you'll have the xml as a file, and the xslt as a file, so PHP or even a batch script could work. You've just got it's just 1 (xml) + 2 (xslt) = 3 (sorted xml)

If you're on Windows want to do it as a batch script, see if you've got MSXSL installed and then it's MSXSL books.xml format.xsl –o out.xml. OSX has Xalan available, and some friendly front-ends.

For PHP's XSLT just follow the examples (and remember to put file:// in front of your filepaths). You'll probably want to cache the result (xslt of this size isn't slow, but as you've got all the files it's really easy to compare the modification dates of your two input files vs the output file).

Any language you choose will probably have a few XSLT implementations available. And if you've got any questions, yadda yadda... :)
posted by holloway at 3:56 PM on August 18, 2004

Oh, and XSLT is a language for manipulating XML that PHP/Java/ASP.NET/* provide so you don't learn the XSL language as implemented in PHP per se, just how to use the XSL library in PHP. XSL is a W3C spec.
posted by holloway at 4:07 PM on August 18, 2004

Shoot, I didn't know you didn't know about XSL, sorry about that. Thankfully holloway, my favourite MeFi-kiwi-hero, saved the day! :)
posted by riffola at 6:35 PM on August 18, 2004

/me clangs his arm cuffs against riffola's and throws a smoke bomb on the ground
posted by holloway at 8:28 PM on August 18, 2004

« Older 30-Days Notice?   |   Confused by Jose Saramago's "The Cave" Newer »
This thread is closed to new comments.