Symmetri Developer Blog

February 5, 2008

XML parsing in AS3

General, Flash/Flex, XML, Javascript - By Shourov Bhattacharya

Parsing XML documents in Actionscript 2 was never much fun. It had the flash.xml.XMLDocument class which did your standard XML DOM traversing and manipulation, but it was always a tedious task to do any sort of complex parsing that way. What a developer really wants is to convert the XML to objects, which would allow us to deal with the parsed data using native data structures such as arrays. I found an excellent AS2 solution written by a guy called Shane MacCartney called Flash XML Remoting which did exactly that, and I have used it successfully in Flash 8 projects which deal with XML data returned from web services.

However, AS3 has made things much easier. We now have access to ECMAScript for XML (E4X), a programming language that makes manipulating XML much more intuitive and appealing. Essentially, it means that we can refer to nodes and attributes within the XML using standard object dot notation without any extra effort. For example, the following example looks up the title attribute of the second book in the BookXml document:

var title:String = BookXml.book[2].@title;

Yet another example of why AS3 is a quantum improvement over AS2. Incidentally, this makes pulling XML data from the server a far more profitable exercise in AS3 than before. It used to be the case that JSON was the preferred method of passing data structures back to Flash, as it took no extra effort to parse JSON in AS. Now, both the JSON and XML options are very close in terms of programming effort. Of course, there may be other valid reasons for choosing one over the other.

October 30, 2007

XML-RPC in PHP

General, PHP, XML - By Shourov Bhattacharya

If you are looking for an implementation of XML-RPC in PHP, I can recommend Keith Deven’s XML-RPC Library for PHP (v 2.5) as an excellent solution. Although the XML-RPC for PHP open source project is more comprehensive and more widely used (I think), the disadvantage that I saw was that it was far too complex to actually use, especially when building the server. Keith Devens’ solution, by contrast, is far cleaner, simpler and easier to understand. And the more software I write, the more I appreciate simplicity as a virtue.

October 22, 2007

MakeXml10Safe()

PHP, XML - By Shourov Bhattacharya

I am writing a Web Service that returns data in simple XML 1.0. The character encoding of the data is UTF-8, but writing the XML output, I found that responses were sometimes throwing errors on the XML parser on the client side, with an "Invalid character in XML …" type error (for example, using the XML parser built into Firefox).

It turns out that my data contains a number of control characters that lie outside the range of valid XML 1.0 characters (see http://www.w3.org/TR/REC-xml/#charsets):

Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

The best solution I found was to write my own "replace" function to convert these characters into character code references:

 /**                                                                
 * Strip out special characters from a string and make it XML 1.0 safe.
 *                                                                  
 * @param string $input, string to clean
 * @return $response, cleaned string
 *
 */  
 function MakeXml10Safe($input)
 {
  // escape common characters
  $output = str_replace(’&’, ‘&’, $input);  
  $output = str_replace(’<’, ‘&lt;’, $output); 
  $output = str_replace(’>’, ‘&gt;’, $output);   
  $output = str_replace(’\n’, ‘’, $output);  
  // escape control codes that are not valid XML 1.0
  $pattern = ‘/[\x-\x8\xb-\xc\xe-\x1f]/’;
  $output = preg_replace($pattern,’&#’.ord(’$0′).’;',$output);
  
  return $output;
 }

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph