|
| 1 | +<?php |
| 2 | +class ArrayToXML { |
| 3 | + /** |
| 4 | + * Build an XML Data Set |
| 5 | + * |
| 6 | + * @param array $data Associative Array containing values to be parsed into an XML Data Set(s) |
| 7 | + * @param string $startElement Root Opening Tag, default data |
| 8 | + * @param string $xml_version XML Version, default 1.0 |
| 9 | + * @param string $xml_encoding XML Encoding, default UTF-8 |
| 10 | + * @return string XML String containig values |
| 11 | + * @return mixed Boolean false on failure, string XML result on success |
| 12 | + */ |
| 13 | + public static function buildXML($data, $startElement = 'data', $xml_version = '1.0', $xml_encoding = 'UTF-8'){ |
| 14 | + if(!is_array($data)){ |
| 15 | + $err = 'Invalid variable type supplied, expected array not found on line '.__LINE__." in Class: ".__CLASS__." Method: ".__METHOD__; |
| 16 | + trigger_error($err); |
| 17 | + //if($this->_debug) echo $err; |
| 18 | + return false; //return false error occurred |
| 19 | + } |
| 20 | + $xml = new XmlWriter(); |
| 21 | + $xml->openMemory(); |
| 22 | + $xml->startDocument($xml_version, $xml_encoding); |
| 23 | + $xml->startElement($startElement); |
| 24 | + |
| 25 | + /** |
| 26 | + * Write keys in $data prefixed with @ as XML attributes, if $data is an array. |
| 27 | + * When an @ prefixed key is found, a '%' key is expected to indicate the element itself, |
| 28 | + * and '#' prefixed key indicates CDATA content |
| 29 | + * |
| 30 | + * @param object $xml XMLWriter Object |
| 31 | + * @param array $data with attributes filtered out |
| 32 | + */ |
| 33 | + function writeAttr(XMLWriter $xml, $data) { |
| 34 | + if(is_array($data)) { |
| 35 | + $nonAttributes = array(); |
| 36 | + foreach($data as $key => $val) { |
| 37 | + //handle an attribute with elements |
| 38 | + if($key[0] == '@') { |
| 39 | + $xml->writeAttribute(substr($key, 1), $val); |
| 40 | + } else if($key[0] == '%') { |
| 41 | + if(is_array($val)) $nonAttributes = $val; |
| 42 | + else $xml->text($val); |
| 43 | + } elseif($key[0] == '#') { |
| 44 | + if(is_array($val)) $nonAttributes = $val; |
| 45 | + else { |
| 46 | + $xml->startElement(substr($key, 1)); |
| 47 | + $xml->writeCData($val); |
| 48 | + $xml->endElement(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + //ignore normal elements |
| 53 | + else $nonAttributes[$key] = $val; |
| 54 | + } |
| 55 | + return $nonAttributes; |
| 56 | + } |
| 57 | + else return $data; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Write XML as per Associative Array |
| 62 | + * @param object $xml XMLWriter Object |
| 63 | + * @param array $data Associative Data Array |
| 64 | + */ |
| 65 | + function writeEl(XMLWriter $xml, $data) { |
| 66 | + foreach($data as $key => $value) { |
| 67 | + if(is_array($value) && isset($value[0])) { //numeric array |
| 68 | + foreach($value as $itemValue){ |
| 69 | + if(is_array($itemValue)) { |
| 70 | + $xml->startElement($key); |
| 71 | + $itemValue = writeAttr($xml, $itemValue); |
| 72 | + writeEl($xml, $itemValue); |
| 73 | + $xml->endElement(); |
| 74 | + } else { |
| 75 | + $itemValue = writeAttr($xml, $itemValue); |
| 76 | + $xml->writeElement($key, "$itemValue"); |
| 77 | + } |
| 78 | + } |
| 79 | + } else if(is_array($value)) { //associative array |
| 80 | + $xml->startElement($key); |
| 81 | + $value = writeAttr($xml, $value); |
| 82 | + writeEl($xml, $value); |
| 83 | + $xml->endElement(); |
| 84 | + } else { //scalar |
| 85 | + $value = writeAttr($xml, $value); |
| 86 | + $xml->writeElement($key, "$value"); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + writeEl($xml, $data); |
| 91 | + |
| 92 | + $xml->endElement();//write end element |
| 93 | + //returns the XML results |
| 94 | + return $xml->outputMemory(true); |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments