How to output data in xml?

Seeking (free?) applications that can sort XML data by a node of your choice?

  • 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.

  • Answer:

    Just use XSL to sort the file.

grumblebee at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

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.

malphigian

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.

grumblebee

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.

rglasmann

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.)

cell

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.

grumblebee

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.

plemeljr

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.

grumblebee

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 http://www.soggybeaver.com/blah/xml_sort_test.txt into a blank movie if you want to see)

cell

Link to the relevent php manual page http://www.php.net/manual/en/ref.xml.php. 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)

toby\flat2

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.