function outputXml($results, $xsltPath) { /* Setting XML header */ @header("content-type: text/xml; charset=UTF-8"); /* Initializing the XML Object */ $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->setIndentString(' '); $xml->startDocument('1.0', 'UTF-8'); if (isset($xsltPath)) { $xml->WritePi('xml-stylesheet', 'type="text/xsl" href="' . $xsltPath . '"'); } $xml->startElement('callback'); $xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $xml->writeAttribute('xsi:noNamespaceSchemaLocation', 'schema.xsd'); /* Function that converts each array element to an XML node */ function write(XMLWriter $xml, $data) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_numeric($key)) { #The only time a numeric key would be used is if it labels an array with non-uniqe keys write($xml, $value); continue; } else { $xml->startElement($key); write($xml, $value); $xml->endElement(); continue; } } $xml->writeElement($key, $value); } } /* Calls previously declared function, passing our results array as parameter */ write($xml, $results); /* Closing last XML node */ $xml->endElement(); /* Printing the XML */ echo $xml->outputMemory(true); }