In part 1 of this 2 part article, I showed how to put data in your XML file through the use of entity references. I also talked about the BOM, UTF-8 encoding and how to save your XML files in e.g. NotePad. What I want to do today is talk about how you can generate XML on the fly using both ASP and PHP.
What about server side generated xml data?
One trick that is often used is to pass in the URL of a server side script in the load() method of the XML object in Flash. That server side script can be a ASP, PHP, JSP or WEP (whatever page - ok, bad joke). What this kind of script actually does is create XML data and write it to the screen. Flash only wants XML data to be passed to an XML object and doesn't care if it's read from a static XML file or from data that is generated dynamically.
How do I create XML data on the server?
Probably the most used technique here is to compose a string with XML data and write it to the screen.
-
var xmlOutput = new String("<person>John Petrucci</person>");
-
Response.Write(xmlOutput)
This is bad for 2 reasons:
1. You need to make sure that when you open a node, you also close it further in the string. When you're dealing with a lot of XML data things get very messy and you'll most likely forget to close one.
2. As covered in part 1, you need to replace special characters (remember them?) with entity references. What you can do to achieve this, is create a function or a prototype when you are using asp/jscript and call that function or prototype on every text you insert. This may not sound like a disadvantage because you have to replace them anyway, but you'll see that this can done much cleaner and with less work. In addition, you can easily forget to send the text through that function.
-
//String prototypes to encode the XML data.
-
String.prototype.replaceCharacter = function(target, replacement){
-
var re = new RegExp(target, "gi");
-
var out = this.replace(re, replacement);
-
-
return out;
-
}
-
-
String.prototype.encode = function(){
-
var out = this;
-
-
out = out.replaceCharacter("&", "&");
-
out = out.replaceCharacter("<", "<");
-
out = out.replaceCharacter(">", ">");
-
out = out.replaceCharacter("'", "'");
-
out = out.replaceCharacter(""", """);
-
-
return out;
-
}
-
var xmlData = new String("<?xml version='1.0'?>");
-
-
//data pulled from a recordSet
-
xmlData += "<data>" + myRecordSet.Fields("someField").Value.encode() + "</data>";
-
-
Response.Write(xmlData);
Using XML DOM
A better way of composing XML data would be to use the XML DOM object. When you're dealing with ASP, you can use Microsoft.XMLDOM. PHP has a DomDocument you can use. Here's an ASP code example.
-
var xmlDoc = Server.CreateObject("Microsoft.XMLDOM");
-
-
//create a processing instruction
-
//utf-8 is the default and should not be specified
-
var pi = xmlDoc.createProcessingInstruction("xml", "version=\"1.0\"");
-
xmlDoc.appendChild(pi);
-
-
//create the rootnode and append it to the xml object
-
var rootNode = xmlDoc.createElement("idiomatic");
-
rootNode.setAttribute("version", "1.0");
-
xmlDoc.appendChild(rootNode);
-
-
//create the courseNode
-
var courseNode = xmlDoc.createElement("course");
-
courseNode.setAttribute("label", objRdsTree.Fields("title").Value);
-
rootNode.appendChild(courseNode);
-
-
//sets the rootNode (documentElement)
-
xmlDoc.documentElement = rootNode;
Note that is it not needed to call the encode function on the data you pass in. This is handled automatically by the XML DOM object. You also can't forget to close nodes, since you're not using strings.
To get the XML data on screen (or load it into Flash), you can call Response.Write() and pass in the xmlDoc.xml property. This will give you a string representation of its data. Also, don't forget to set the codepage of your ASP page to UTF-8. This is the 65001 in the code below.
-
Response.Write(xlmDoc.xml);
-
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
Generating XML files
What if you want to save your XML data in a file on the server? The best way I found is to write a file using ADODB.Stream. That way, you can save your file under UTF-8 encoding which is exactly what we need.
-
function saveXmlToFile(xmlDoc, fileName){
-
var adoStream = Server.CreateObject("ADODB.Stream");
-
adoStream.Open();
-
adoStream.Charset = "UTF-8";
-
adoStream.WriteText(xmlDoc.xml);
-
adoStream.SaveToFile(Server.MapPath("xml/" + fileName));
-
adoStream.Close();
-
}
If you're using PHP, you can save UTF-8 files using the pack() function which lets you pack data into binary strings.
When you saved your file on the server, download and open it (notepad) and check its encoding. It should be UTF-8.
Wrapping up
That's about it. I hope this article helped you (in whatever way). Feel free to contact me in case you have something to add or came up with other solutions.
Thank you and goodnight!
Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
Christophe Herreman is a software developer living in Belgium. He's working on high-end Flex and AIR solutions at 
June 8th, 2006 at 2:15 am
I am trying to integrate your tutorial on getting XML ready for Flash. I have a News Writer put together with Flash, PHP, and XML but I need to place it on a Windows server (.NET) .
Would you know how to convert the following PHP snippet to ASP? (It is very close to what you have above (http://www.herrodius.com/blog/?p=18). I just need to “WRITE” an XML file. Overwriting is fine and I do have permissions set to do so.)
— start code —
— end code —
Thank you Christophe.
- Ken
http://www.xzone9.com
June 25th, 2006 at 7:15 pm
I was able to get an ASP version for wiriting XML from Flash as shown above in PHP.
Painstaking but now it works while receiving some great help form another developer. If anyone would like an exmaple of this script, please email me. Thank you.
ken at xzone9 dot com
September 26th, 2006 at 7:10 pm
Great tutorial! I’m seeking advice on how to save data to an Access database using an XML-file returned from Flash. I guess that what I have to do is to parse the XML-file returned, create an SQL INSERT statement and insert… However, I’m having a little trouble doing this. Is there an easier way, or does anyone know of a tutorial or similar somewhere on the net?
Br Bo
November 13th, 2006 at 6:13 am
I am currently using PHP DomDocument to create XML from DataBases; the “pack” function makes the difference.
Since I am a “latin-characters” native speaker, I have to create perfectly readable UTF-8 XML files. This is the best solution I’ve came across the web tonite.
Thank you very much!!