Last active
September 15, 2017 11:58
-
-
Save Werninator/d61419179ae23083706f5be443cb8b14 to your computer and use it in GitHub Desktop.
[php] A basic little helper to convert assoc arrays to xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AssocArrayToXmlParser | |
{ | |
private $assocArray; | |
private $xmlVersion = '1.0'; | |
private $encoding = 'utf-8'; | |
function __construct(array $_assocArray = null) | |
{ | |
if (!$_assocArray) | |
return; | |
$this->setArray($_assocArray); | |
} | |
public function getArray() | |
{ | |
return $this->assocArray; | |
} | |
public function setArray(array $_assocArray = null) | |
{ | |
if (count(array_filter(array_keys($_assocArray), 'is_string')) > 0) | |
$this->assocArray = $_assocArray; | |
} | |
public function parse() | |
{ | |
reset($this->assocArray); | |
$firstKey = key($this->assocArray); | |
$xml = '<?xml version="' . $this->xmlVersion . '" encoding="' . $this->encoding . '"?>' . PHP_EOL; | |
$xml .= $this->parseAssocNodeToXml($firstKey, $this->assocArray[$firstKey]); | |
return $xml; | |
} | |
private function parseAssocNodeToXml($name, $contents, $intendation = '') | |
{ | |
$attributes = []; | |
$nameExplode = explode('[', $name); | |
$name = $nameExplode[0]; | |
foreach ($nameExplode as $i => $expl) | |
if ($i !== 0) | |
$attributes[] = str_replace(']', '', $expl); | |
$attributes = count($attributes) | |
? ' ' . implode(' ', $attributes) | |
: ''; | |
$contentsIsArray = is_array($contents); | |
if ($contents === '') | |
return $intendation . '<' . $name . $attributes . ' />' . PHP_EOL; | |
if ($attributes != ' ') { | |
if ($contentsIsArray) { | |
$tmp = []; | |
foreach ($contents as $contentName => $content) | |
$tmp[] = $this->parseAssocNodeToXml($contentName, $content, $intendation . "\t"); | |
$contents = PHP_EOL . implode('', $tmp); | |
} | |
return $intendation . '<' . $name . $attributes . '>' . $contents . ($contentsIsArray ? $intendation : '') . '</' . $name . '>' . PHP_EOL; | |
} else { | |
$tmp = []; | |
foreach ($contents as $content) | |
$tmp[] = $this->parseAssocNodeToXml($name, $content, $intendation); | |
return implode('', $tmp); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$assocArrayXmlMessage = [ | |
'MyXMLMessage[version="1.0"][type="isPrettyCool"]' => [ | |
'DateOfExport' => date('Y-m-d H:i:s'), | |
'SelfClosingTag' => '', | |
'SomethingWhichRepeats[]' => [ | |
[ | |
'Id' => 1, | |
], | |
[ | |
'Id' => 2, | |
] | |
] | |
] | |
]; | |
$parser = new AssocArrayToXmlParser($assocArrayXmlMessage); | |
$xmlString = $parser->parse(); | |
/* | |
Output: | |
<?xml version="1.0" encoding="utf-8"?> | |
<MyXMLMessage version="1.0" type="isPrettyCool"> | |
<DateOfExport>2016-10-31 10:49:22</DateOfExport> | |
<SelfClosingTag /> | |
<SomethingWhichRepeats> | |
<Id>1</Id> | |
</SomethingWhichRepeats> | |
<SomethingWhichRepeats> | |
<Id>2</Id> | |
</SomethingWhichRepeats> | |
</MyXMLMessage> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment