Error converting Array to XML

GroundHawk

New Member
I am building an API. One of the functions returns a multi-dimensional array with a dynamic number of records. When a single records is returned my array2xml function works fine but when there is more than 1 I get the error "XML Parsing Error: junk after document element". I have included the function that should convert it to xml and the array in it's unconverted format below:\[code\]/* Setting XML header */ @header ("content-type: text/xml charset=utf-8"); /* Initializing the XML Object */ $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $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)){ $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, $data); /* Closing last XML node */ $xml->endElement(); /* Printing the XML */ echo $xml->outputMemory(true);\[/code\]The array being converted:\[code\]Array( [0] => Array ( [Scope_CH] => Intruder Alarm Systems (ACPO) Certified [Scope_ID] => 1 ) [1] => Array ( [Scope_CH] => CCTV Systems Approved [Scope_ID] => 5 ))\[/code\]The output if I remove the header from the xml creator is this:\[code\]<?xml version="1.0" encoding="UTF-8"?><callback xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd"><Scope_CH>Intruder Alarm Systems (ACPO) Certified </Scope_CH><Scope_ID>1</Scope_ID></callback><Scope_CH>CCTV Systems Approved </Scope_CH><Scope_ID>5</Scope_ID>\[/code\]
 
Back
Top