Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175

    Trasformare XML in Array

    Avrei bisogno di una funzione che trasformi un documento XML in un array. Un po' come avviene con json_decode, che trasforma un JSON in un array.
    Esiste una funzione adatta in PHP o devo affidarmi ad una ricerca su google? Per la seconda ipotesi qualcuno conosce un buon algoritmo?

    Saluti
    PS: Ovviamente anche una funzione nativa che trasformi XML in JSON sarebbe utile.

  2. #2
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    Ciao, non so quanto complsso sia il tuo xml ma io per i miei xml classici... tag e testo uso questo

    Codice PHP:
        function getDictionaryValue($section="",$xmlFile="")
        {
            
    $xml simplexml_load_file($xmlFile);        
            if(
    $section != "")
            {              
                return 
    self::object2array($xml->{$section});
            }
            else
            {   
                return 
    $xml;
            }
        }    

        function 
    object2array($object)
        {
           
    $return NULL;
           if(
    is_array($object))
           {
               foreach(
    $object as $key => $value)
                   
    $return[$key] = self::object2array($value);
           }
           else
           {
               
    $var get_object_vars($object);
               if(
    $var)
               {
                   foreach(
    $var as $key => $value)
                       
    $return[$key] = ($key && !$value) ? NULL self::object2array($value);
               }
               else return 
    $object;
           }
           return 
    $return;
        } 
    xml e' il nome dell xml appunto e section e' il tag da prendere(se non passo la sezione prendo tutto)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.