Created
December 12, 2018 09:59
-
-
Save kode54/ef7fb4ddaeed54eb2561ef286a1e44c1 to your computer and use it in GitHub Desktop.
SimpleXML helper for PHP, converts XML object notation to more easily handled array notation.
This file contains 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
function xmlObjToArr($obj) { | |
$namespace = $obj->getDocNamespaces(true); | |
$namespace[NULL] = NULL; | |
$children = array(); | |
$attributes = array(); | |
$name = strtolower((string)$obj->getName()); | |
$text = trim((string)$obj); | |
if( strlen($text) <= 0 ) { | |
$text = NULL; | |
} | |
// get info for all namespaces | |
if(is_object($obj)) { | |
foreach( $namespace as $ns=>$nsUrl ) { | |
// atributes | |
$objAttributes = $obj->attributes($ns, true); | |
foreach( $objAttributes as $attributeName => $attributeValue ) { | |
$attribName = strtolower(trim((string)$attributeName)); | |
$attribVal = trim((string)$attributeValue); | |
if (!empty($ns)) { | |
$attribName = $ns . ':' . $attribName; | |
} | |
$attributes[$attribName] = $attribVal; | |
} | |
// children | |
$objChildren = $obj->children($ns, true); | |
foreach( $objChildren as $childName=>$child ) { | |
$childName = strtolower((string)$childName); | |
if( !empty($ns) ) { | |
$childName = $ns.':'.$childName; | |
} | |
$children[$childName][] = xmlObjToArr($child); | |
} | |
} | |
} | |
return array( | |
'name'=>$name, | |
'text'=>$text, | |
'attributes'=>$attributes, | |
'children'=>$children | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment