All rights reserved. Licenced under GPL 2. Feel free to reimplement ArrayML in other languages. I'm sure those with more patience could use the PHP4 DOM XML module, or, worse yet, strings. For more information, see: */ /* How to use: The arrayml class takes one or two arrays. The first array is the associative array you want converted The second array takes some commands. They are detailed below. To get data out of arrayml, use the xml() or dom() methods to get back an XML string or DOM object respectively. Fave fun XSLTing. $opts - second array commands. use the part in 'quotes' as the key, and set as true. omit if you do not want to use. - 'addatts' = true # adds 1+2 below OR - 'attsonly' = true # adds 1+2+3+4 below OR { 1 'attributes_key' = true 2 'attributes_val' = true 3 'no_text_key' = true 4 'no_text_val' = true } attributes_[key|val] adds a @c attribute to each element. no_text_[key|val] removes the key from the next node. */ class arrayml { function __construct($array, $opts = NULL) { if (isset($opts)) { if ($opts['attsonly'] == true) { $this->opts = array('attributes_key' => true, 'attributes_val' => true, 'no_text_key' => true, 'no_text_val' => true); } elseif ($opts['addatts'] == true) { $this->opts = array('attributes_key' => true, 'attributes_val' => true); } else { $this->opts = $opts; } } $this->array = $array; $this->dom = new DOMDocument('1.0', 'UTF-8'); $rootelement = $this->dom->createElement('array'); $this->dom->appendChild($rootelement); $this->root = $this->dom->documentElement; $this->parse($this->root, $this->array); } function parse($root, $arr) { foreach ($arr as $k => $v) { // key $node = $this->dom->createElement("node"); $kname = "key"; if (isset($k) && !isset($v)) { $kname = "val"; } $key = $this->dom->createElement($kname); if ($this->opts['no_text_key'] != true) { $keytext = $this->dom->createTextNode($k); $key->appendChild($keytext); } if ($this->opts['attributes_key'] == true) { $key->setAttribute("c", $k); } $node->appendChild($key); // val if (is_array($v)) { $val = $this->dom->createElement("val"); $this->parse($val, $v); $node->appendChild($val); } else { $val = $this->dom->createElement("val"); if ($this->opts['no_text_val'] != true) { $valtext = $this->dom->createTextNode($v); $val->appendChild($valtext); } if ($this->opts['attributes_val'] == true) { $val->setAttribute("c", $v); } $node->appendChild($val); } $root->appendChild($node); } } function xml() { return $this->dom->saveXML(); } function dom() { return $this->dom; } } ?>