<?php class XMLCreate { private $DOMDocument; private $DOMElement; { $this -> DOMDocument = new DOMDocument ('1.0', 'UTF-8'); $this -> createNode ($this -> DOMDocument -> appendChild ($this -> DOMDocument -> createElement ($rootNode)), $array); } { foreach ($data as $key => $value) { { $this -> createNode ($DOMElement -> appendChild ($this -> DOMDocument -> createElement ($key)), $value); } { } else { $DOMElement -> appendChild ($this -> DOMDocument -> createElement ($key, $value)); } } } public function save () { return $this -> DOMDocument -> saveXML (); } } ?>
Sposób użycia:
<?php ( '@time' => time (), // @ - oznacza że dany index będzie atrybutem elementu, a wartość index'u zostanie zmieniona na wartość atrybutu. ( 'name' => 'Jan', 'surname' => 'Kowalski', ( 'city' => 'Tarnow', 'postalCode' => '33-100', ), '0interests' => array // Jak wiadomo nazwa elementu nie może zaczynać się od cyfry. Początkowe cyfry zos aną usunięte i zostanie stworzonych kilka elementów o tej samej nazwie. ( 'name' => 'Informatyka', ), ( 'name' => 'Sport', ), ), ); $XML = new XMLCreate ($array); // Jako drugi, nieobowiązkowy argument możemy podać nazwę głównego węzła. ?>