DOMDocument class
<?php
$data = [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Mary'],
['id' => 3, 'name' => 'Robert'],
];
$doc = new DOMDocument();
$doc->encoding = 'UTF-8';
$persons = $doc->createElement('persons');
foreach ($data as $item) {
$person = $doc->createElement('person');
$person->setAttribute('id', $item['id']);
$name = $doc->createElement('name', $item['name']);
$person->appendChild($name);
$persons->appendChild($person);
}
$doc->appendChild($persons);
$doc->save('test.xml');
Leave a Comment
Cancel reply