0

I have php array like below

$row = array (
  'name' => 'david',
 'bio' => 'good man'
 );

i want to convert this array to corresponding XML page like scenario below.

 <?xml version="1.0" encoding="UTF-8"?>
 <note>
   <name>david</name>
   <bio>good man</bio>
</note>
Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77
Jishad
  • 89
  • 1
  • 2
  • 8

2 Answers2

1

try like this.

   $xml = new SimpleXMLElement('<note/>');
   $row = array_flip($row);
   array_walk_recursive($row, array ($xml, 'addChild'));
   echo htmlentities($xml->asXML());
Jishad
  • 89
  • 1
  • 2
  • 8
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
0

Generate XML with DOM:

$row = array (
  'name' => 'david',
  'bio' => 'good man'
);

$dom = new DOMDocument();
$dom->formatOutput = TRUE;

$note = $dom->appendChild($dom->createElement('note'));
foreach ($row as $name => $value) {
  $note
    ->appendChild($dom->createElement($name))
    ->appendChild($dom->createTextNode($value));
}

echo $dom->saveXml(); 

Output:

<?xml version="1.0"?>
<note>
  <name>david</name>
  <bio>good man</bio>
</note>
ThW
  • 19,120
  • 3
  • 22
  • 44