Search code examples
phpxmlxmldom

Error on php XML DOM


I got an error that I can't solve.

$unsortedArray = array (array ( 'profileId' => '32', 'profileMiniature' => '32' ,'firstName' => 'Sarmad', 'lastName' => 'Nekomanesh', 'numberOfMutualContacts' => '0' ), array ( 'profileId' => '37', 'profileMiniature' => '37', 'firstName' => 'Sophie', 'lastName' => 'Gronlund', 'numberOfMutualContacts' => '0' ) );

header('Content-type: text/xml');
$xml = new DOMDocument('1.0', 'utf-8');


foreach($unsortedArray as $v){
    $profiles = $xml->createElement('profiles');
    $xml->appendChild($profiles);
    $profile = $xml->createElement('profile');
    $profile->setAttribute('profileId', $v['profileId']);
    $profile->setAttribute('profileMiniature', $v['profileMiniature']);
    $profile->setAttribute('firstName', $v['firstName']);
    $profile->setAttribute('lastName', $v['lastName']);
    $profiles->appendChild($profile);
}


$xml->formatOutput = true;
echo $xml->saveXML();

And when I only got one array element in the array I got no error at all.


Solution

  • I ran your lines of code (copy&paste) and got no errors. It printed valid XML document. You probably have errors in other code.