Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $xmlstring = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>
- <multimidia>
- <image>
- <image-url>
- teste
- </image-url>
- </image>
- </multimidia>';
- $xml = simplexml_load_string($xmlstring);
- $array = xmlObjToArr($xml);
- echo "<pre>";
- var_dump($array);
- 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
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement