-
lemon juice hurts your eyes
[RESOLVED] Parsing data from String passed by php
So I have now tested my file with a test xml and it works just fine, but I need to get the dat from the server so it becomes dynamic.
I've gotten some php to generate a xml document, but I can't get it to "be" an xml, it's just formatted like it and it's a String.
Can I somehow convert the String to a XML variable, or will I have to cut up the string and get out the info I need.
In the second case, what would be the best way to parse the data in php (what formatting)?
The xml looks like this:
Code:
<portfolio>
<project title="a name" info="Bla" thumb="files/thumbs/p1.gif">
<file title="a name" info="blabla" thumb="files/thumbs/p1_1.gif" url="files/project_files/p1_1.gif" />
<file title="a name" info="blabla" thumb="files/thumbs/p1_2.gif" url="files/project_files/p1_2.gif" />
</project>
<portfolio>
And with all those < > " = symbols I think I would have a hard time to break it apart.
The result should become an Array which contains project Objects with properties and another Array to hold file Objects with their own properties.
But the general question is: String to XML, how? If not possible, how to build this xml shaped String so it can be picked apart easily?
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
Generally I do something like:
PHP Code:
public function load( url:String ):void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener( Event.COMPLETE, onURLLoaderComplete);
urlLoader.load( new URLRequest( url ) );
}
public function onURLLoaderComplete( e:Event ):void {
var urlLoader:URLLoader = URLLoader( e.target );
var xml:XML = new XML( urlLoader.data );
urlLoader.removeEventListener( Event.COMPLETE, onURLLoaderComplete);
}
Last edited by v5000; 08-17-2009 at 12:04 PM.
-
lemon juice hurts your eyes
But why re-store the data received in a new URLLoader?
If I trace e.target.data I get the XML String in the output panel too..
I'm really really bad at php, so I managed to put together a file which gets information out of a database and I managed to get a decent looking xml structure.
I don't have enough knowledge to put things into Arrays and send that over to Flash, so I'll have to do it the dirty, sloppy way..
Just need a decent structure for the String to appear in
Anyone good with String manipulation? (or how to get a String into an XML somehow)
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
var xml:XML = new XML( urlLoader.data );
The example does not create a new URLLoader, it casts the event target to a URLLoader, big difference. With the above, there is no need to store a ref to the URLLoader since it's a throw away object, but that's beside the piont. If you want to parse a string into xml then just pass the string to the a new XML constructor.
-
lemon juice hurts your eyes
Ok, I got that, but I'm using OOP so I have a URLLoader at my disposal so I don't need to pass in the e.data into a new URLLoader and then get it from there, right?
Anyway, I'll try that out tomorrow and post back
Thanks!
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
uhm, flash is OO, we're all using OOP. If the URLLoader is used only once, then its a watse of memory to store it as a class property when you can retrieve the ref from the listener and let the URLLoader get gc'ed. All you care about is the data right? Why leave a deadbeat URLLoader hanging around? Anyway, its nitpicking and comes down to what objects do you really need to persist and how militant you are about memory usage. I'm sure you'll figure out something that works.
-
Senior Member
the old data is gone as soon as there's no reference to it anymore; if it's a class level variable then it's gone at the end of the function running it; if it's a class property, it's gone as soon as you dereference the class. I've written stuff that handles huge amounts of data transfer in/out and all you gotta know is, don't hold direct references to objects connected with the result set and you'll be fine.
Look into amfphp if you're serious about doing any kind of complex dataset transfers from php to flash. XML is fine for small scale stuff, but if it's anything more complicated than a basic list you'll save time and energy in the end by writing it into a database and sending it through with AMF.
J
-
lemon juice hurts your eyes
Yeah, but I can't seem to wrap my head around AMF I find it really hard to use, since the tutorials s*ck and are outdated..
But there's been a post on Flashtuts about that so I'll check that one out before trying to do stupid things
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
lemon juice hurts your eyes
Ok, so I've read the tutorial (found here) and I've installed it onto my local server and I've gotten the HelloWorld.php example to work in a basic app as instructed in the tut.
But now that I see how easy it is to call the methods from AS3, It would be easier to get the data from the database directly, rather than generating an XML, as said above by previous posters.
Only problem is my lack of knowledge of php
The project I'm working parses the XML into something looking like this:
Code:
array[
object{title:String,description:String,thumburl:String,files:Array[
object{title:String,description:String,thumburl:String,url:String}
object{title:String,description:String,thumburl:String,url:String}
object{title:String,description:String,thumburl:String,url:String}
// etc
] // end files array
} // end project object
// and more object beneath here
]
Would it be possible to pass something like this into my project without the need of parsing it in AS3 (so construct it in php)?
I know there are things as Arrays and Objects in php, but will my AS3 recognize it as the same type?
EDIT: So I've gotten the code V5000 posted to work after trying it for a third time right now, but I'd still like to know the answer to the questions above, so I can do it better next time
Last edited by florianvanthuyn; 08-19-2009 at 06:06 AM.
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
Senior Member
Would it be possible to pass something like this into my project without the need of parsing it in AS3 (so construct it in php)?
That's what AMFPHP is for. It just passes the objects into Flash as big binary constructs that can be arrays, objects, recordsets... whatever you have in PHP you just return it in the function you call from Flash. It's also much, much faster than transfering the same data in XML.
-
lemon juice hurts your eyes
Thanks
Whenever I have to use it in the future and I'm not getting to where I want to get, than I'll make a new thread for that.
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|