Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function toXML($object, $rootName="", $indexName="") {
- $xmlDoc = new DOMDocument();
- if (!is_string($object)) { //we gotta hope its an array
- if ($rootName) {
- $root = $xmlDoc->appendChild(
- $xmlDoc->createElement($rootName));
- }
- $arrayParse = function($array, $root) use (&$arrayParse, &$xmlDoc, $indexName) {
- foreach ($array as $key => $val) {
- if (is_array($val)) {
- if (is_numeric($key) && $indexName) {
- $newRoot = $root->appendChild(
- $xmlDoc->createElement($indexName));
- } else if (is_numeric($key)) {
- $newRoot = $root->appendChild(
- $xmlDoc->createElement("Object"));
- } else {
- $newRoot = $root->appendChild(
- $xmlDoc->createElement($key));
- }
- $arrayParse($val, $newRoot);
- } else {
- $root->appendChild(
- $xmlDoc->createElement($key, $val));
- }
- }
- };
- if (isset($root)) {
- $arrayParse($object, $root);
- } else {
- $arrayParse($object, $xmlDoc);
- }
- } else {
- if ($rootName) {
- $root = $xmlDoc->appendChild(
- $xmlDoc->createElement($rootName, $object));
- } else {
- $root = $xmlDoc->appendChild(
- $xmlDoc->createElement("Object", $object));
- }
- }
- $xmlDoc->formatOutput = true;
- return $xmlDoc->saveXML();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement