daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $xmlstring = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>
  4. <multimidia>
  5.  <image>
  6.    <image-url>
  7.    teste
  8.    </image-url>
  9.  </image>
  10. </multimidia>';
  11.  
  12. $xml = simplexml_load_string($xmlstring);
  13. $array = xmlObjToArr($xml);
  14. echo "<pre>";
  15. var_dump($array);
  16.  
  17. function xmlObjToArr($obj) {
  18.     $namespace = $obj->getDocNamespaces(true);
  19.     $namespace[NULL] = NULL;
  20.  
  21.     $children = array();
  22.     $attributes = array();
  23.     $name = strtolower((string)$obj->getName());
  24.  
  25.     $text = trim((string)$obj);
  26.     if( strlen($text) <= 0 ) {
  27.         $text = NULL;
  28.     }
  29.  
  30.     // get info for all namespaces
  31.     if(is_object($obj)) {
  32.         foreach( $namespace as $ns=>$nsUrl ) {
  33.             // atributes
  34.             $objAttributes = $obj->attributes($ns, true);
  35.             foreach( $objAttributes as $attributeName => $attributeValue ) {
  36.                 $attribName = strtolower(trim((string)$attributeName));
  37.                 $attribVal = trim((string)$attributeValue);
  38.                 if (!empty($ns)) {
  39.                     $attribName = $ns . ':' . $attribName;
  40.                 }
  41.                 $attributes[$attribName] = $attribVal;
  42.             }
  43.  
  44.             // children
  45.             $objChildren = $obj->children($ns, true);
  46.             foreach( $objChildren as $childName=>$child ) {
  47.                 $childName = strtolower((string)$childName);
  48.                 if( !empty($ns) ) {
  49.                     $childName = $ns.':'.$childName;
  50.                 }
  51.                 $children[$childName][] = xmlObjToArr($child);
  52.             }
  53.         }
  54.     }
  55.  
  56.     return array(
  57.         'name'=>$name,
  58.         'text'=>$text,
  59.         'attributes'=>$attributes,
  60.         'children'=>$children
  61.     );
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top