Tom Morris

ArrayML for PHP5

Introduction Permalink

One of the benefits of using JSON and other small data interchange formats is that they map natively to many of the basic data models in programming languages better than XML does with it's elements and attributes.

Here's an example. Take some JSON and load it in to Python. Then print both the JSON and the Python object to screen. They both look shockingly similar.

This is all well and good, but what if the native data model for your programming language or application is XML? For those people who use XSLT, XML pipelines, Formatting Objects, XPath and so on, having all your data in an array or object serialization that's good for JavaScript isn't that useful.

And so you end up separating out your program logic from your transformations, but end up having to write terribly hacky string concatenation scripts or, worse, DOM methods. Life is far too short to have it mock you with writing out the phrase "getElementsByTagName" or "createElementNS" all day long. And life sucks enough already without having to look at the bastard offchild of such messiness.

Enter ArrayML Permalink

ArrayML is a cheeky name for a process which simplifies all that. Instead of mapping the data to XML, you simply map the data format of associative arrays and other native formats to XML and then use XSLT to transform that 'ArrayML' data to something more useful.

End result? Less time spent farting around with rewriting stuff to XML, more time spent in your XML editor actually getting shit done.

ArrayML elements Permalink

ArrayML is a ridiculously simple language. It has only has a small number of elements and one attribute:

array: the root element, contains node elements

node: each node represents a member of the array and can contain one each of key and val.

key: represents the key.

val: represents the value. Can contain data or more node elements.

@c: If you switch attributes mode on, data will appear in the 'c' attribute on both the key and val elements.

What you choose to put inside the keys and vals is restricted only by your imagination and the syntactical requirements of your language.

In PHP, for instance, keys must be strings or integers (iirc), and values can be strings, numbers and arrays.

PHP implementation Permalink

The PHP implementation is in the form of a class called arrayml.

arrayml has three methods:

__construct, xml and dom.

__construct takes two arguments. Firstly, it takes the associative array which you want to convert. Secondly, it takes an optional associative array listing options.

xml returns an XML string after transformation.

dom returns a DOMDocument object.

PHP implementation: opts arguments Permalink

The PHP implementation can be supplied with arguments by adding them to an associative array. There are four options which can be set, and two presets. You set the options by adding them as a key to the options array, with a boolean true as their value.

The four options:

attributes_key - turns the @c attribute on for keys

attributes_val - turns the @c attribute on for values

no_text_key - turns the text() data off for keys

no_text_val - turns the text() data off for vals.

The two presets are:

addatts - a shortcut for attributes_key and attributes_val

attsonly - a shortcut for all four of the options

Implementation on other platforms Permalink

Feel free to write implementations for other platforms. Where possible, stick to the XML standard laid out above, but if that is not possible, get in contact with me and we can work on something more complex.

The XML format I have defined may not be the greatest things ince sliced bread, but it exists just to serve a very simple purpose.

I originally wanted to implement this in Python, but found it too complex.

Conjured up objections? Permalink

"But this is all so simple. Why do you have to put a name on it?"

Because people like names. I spend a lot of time converting between XML and non-XML formats using a variety of different langauges and so on. So, why not?

It is the JavaScripters, after all, who took XMLHttpRequest and turned it in to 'Ajax' and who now have £3,000 conferences on it... If you want to pay me £3,000 - or even £30 - to come and talk about ArrayML, all the better.

"Just use JSON and JavaScript, XML sucks, you are so out of touch, you back old technology and your arse doesn't smell as nice as mine."

I'll take it under advisement. It's there on my to-do list. You know, the one I store in /dev/null

Schema Permalink

The XML format is tremendously simple and does not seem to need any schema to describe it. Even so, RELAX NG is simple, so I will be providing a RELAX NG as XML schema soon, so that people can validate their markup - and for users of schema-aware XSLT processors.

Download Permalink

arrayml-php5.txt


Blog Talks Glossary Colophon
Last updated: Friday, February 1, 2008 8:46:55 PM