PDA

View Full Version : Binding xml values to a flex application


mythprod
08-17-2008, 06:47 AM
I've been googling this for a while now and I think I'm not finding what I want because I'm not getting my terminology right. I'll bet there's already a lot of code to get me started, I just need help from you all to help me narrow my search (I'm new which is why I sound like I don't know what I'm talking about - because I don't. :-P).

I'm using Flex 3 / air / flash CS3 / AS3 to build a frontend to allow a "normal" (read non-programmer) user to edit and update an existing xml document. I've found text editors already built that I can start with and that's a piece of the puzzle. What I'm not able to find is a way to extract certain pieces of that existing xml and then place those values into text input fields in the flash interface. Basically I want to take the most commonly changed values of that xml document and make it easy for the user to change their playlist without having to get into a xml editor and do it manually - a gui front-end to read, edit and update existing xml documents.

For example in the existing xml document, there's an attribute that asks for the name and path of a background image. This is something that changes often depending on the user so I would like to put it as a text input field in my flash GUI. Once the user edits that existing value (path & filename of the background image) and hits save, I'd like for that value to overwrite the previous value in the xml document (utilizing air of course) and update the new path and filename in its place.

I'm assuming I want to "databind" that xml value with a text input box in flex. I come from more of a C background so given that, I'm assuming I'll want to track the position of my cursor within the document while I'm reading it to read and write to the xml document?

Any links, tutorials, code or otherwise you could help me get pointed in the right direction I'd be most appreciative!

TIA!

mythprod
08-17-2008, 09:06 PM
Let me focus my question: How may I build a flex 3 app so that a user can edit an ini or xml file from a gui?

All of the help I'm finding is how to build a text editor or how to append text. I want to pull values from an ini or xml document and insert them into some gui text boxes for the user to edit, then be able to save back out those values without disturbing the rest of the file.

drkstr
08-17-2008, 11:21 PM
If I understand your problem correctly, I think you just need a crash course in E4X (http://livedocs.adobe.com/flex/3/html/13_Working_with_XML_01.html) which is the XML processing standard used in AS3.

In a nut-shell, you will want to read in your XML file as text, then convert it to workable XML object.

//assuming you used a URLLoader to load your file
var myXML:XML = XML(event.target.data);

From there you can use E4X to parse or update the data.

I'm not sure what you planed on for the implementation, but if you are looking for suggestions on this as well, then maybe you will want to look into using a Tree (http://livedocs.adobe.com/flex/3/html/dpcontrols_8.html) control to display the over all structure of the XML. When a node is selected, you could update an "edit" view which lists all the attributes and text content for that node. When the data is changed, update the data provider (http://livedocs.adobe.com/flex/3/html/dpcontrols_1.html) of the Tree control using E4X. That way when you're ready to write it back to the file, it's as easy as

myXML.toXMLString();

Hope this clears some stuff up for you.

Good Luck!
~Aaron


*EDIT*
I didn't really touch on the .ini aspect of your question. This one would be a bit trickier. What I would do is read in the ini file as text, then use regular expressions (http://livedocs.adobe.com/flex/3/html/12_Using_Regular_Expressions_01.html) to parse it into a binary object. Make all your updates to the binary object, then regenerate the ini file based on the current state of the object. Another method I can think of (less desirable in my opinion), would be to store the entire file in memory as a string, and when you're parsing your values out, store the start and end character indexes to insert/replace the updated data back into the string. This was the technique I used for a spell checker I wrote awhile back, and it was a pain to keep all the indexes synchronized. Doable though.

mythprod
08-18-2008, 04:30 AM
Great info! I'll definitely be getting into the e4x help files immediately.

One question, I'd like to take a crack at your first option for the ini files (thanks for adding that at the end as I have to deal more with ini files than I do xml files, unfortunately). I think I understood everything you said until you got to the part about parsing the text into a binary object?

Thanks again for all of your help!

drkstr
08-18-2008, 10:41 PM
Well I normally don't do this, but I thought it was an interesting problem and decided to give it a whack. Check out the results here: http://www.openbaseinteractive.com/ini_data_test/

You can right click in the app to view the source.

Feel free to use some, all, or none of it. It's a bit kludgy at the moment, but if you can make improvements, post back the source! Or not. up to you.

Any ways, take care!

~Aaron

mythprod
08-24-2008, 06:28 AM
Outstanding! Thanks so much for taking the time to figure this out and post it. I'll take a look right away! :-)