This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
If you ever have tried to load an XML-file that is
located on a different domain than your Flash movie is, you
have most likely noticed that the XML.load() function is
restricted to XML-file that are located on the same domain
as the Flash movie.
This means that you cannot load the XML file
"http://www.site.com/data.xml" into the Flash movie
"http://www.swfdomain.com/flash.swf" with the XML.load()
function.
However, there is a very simple way around this with PHP. Before going through this tutorial, I recommend you to check out the other XML tutorials here at kirupa.com.
Okay, let's get started!
The XML-file we are going to use in this example is going to be very basic, since it's just for demonstration. Although, there is one thing that must not be included in the XML-file! That is the XML declaration tag, which looks something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
If that tag is
included in the XML-file, it will cause a PHP parse error,
since php will treat everything within the <? and ?> as
PHP-code. I will discuss this later on.
Okay, so the XML-file will be this:
<testNode> <testChildnode testVar="blah">Test</testChildNode> </testNode>
Just copy & paste this into Notepad, Dreamweaver or any other program capable of editing XML.
Save the file as test.xml.
Creating the PHP
The PHP-file will also be very simple. We will use the
include() function to load the XML-file into the PHP-script.
<?php include($_GET['xmlsource']); ?>
To make it a little bit more dynamic, we will assign the path to the XML-file using the GET method. So when we load the PHP from the Flash movie, it will look like this:
loadXML.php?xmlsource=test.xml
Save the PHP-script as loadXML.php.
|
|
XML Declaration Tag |
|
As I said earlier, we cannot write the XML declaration tag in the XML file, but we can write it in the PHP file, before we include the XML. If you want to use the XML declaration tag, do like this:
|
|
Again, this will be just a matter of copying and pasting.
These are the actions that are required to make this work:
(remove the linebreak on line two)
example_xml.ignoreWhite = true; example_xml.load("http://www.azure.se/loadXML.php? xmlsource=test.xml"); example_xml.onLoad = function (success) { if (success) { trace(example_xml); } };
As you can see in the load()
function, we set the GET-variable xmlsource (that is used in
the PHP-script) to be the same as the name of the XML-file.
In this example, I use a PHP file that is located on the
same domain as the XML-file.
If you want to have the PHP-file on the same domain as your
Flash movie, you can do like this instead:
(again, remove the linebreak)
example_xml.load("loadXML.php? xmlsource=http://www.azure.se/test.xml");
Save the Flash movie as xmlExample.fla, and you're set!
Now it's just for you to
upload the files to the webserver(s) and to testrun
everything.
But be aware of that the trace() function in the Flash movie
will not display anything if you are running the SWF file in
a HTML document or in the standalone player.
|
|
Extra Information |
|
Alternate,
Secure Method - by njs12345
Using the example PHP script provided in the tutorial, it is possible to include any file. For instance, under Unix, it is possible to include the file /etc/passwd, which contains passwords for the whole system. This could also be used to include the source code of other PHP documents. For instance, how would you like it if someone included your PHP connection strings? It would be much better to check if the URL string begins with "http://" using some code like this:
|
|
Source
If you have any thoughts or questions about this tutorial, or about anything else for that matter, feel free to post in kirupaForum! And don't forget to search before posting, your question might already have been answered!
Good Luck and Happy Flashing! :D
Kalliban
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
:: Copyright KIRUPA 2026 //--