Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Problema con conversione JSON in XML

    ciao!

    avrei la necessità di convertire JSON in XML.
    questo un esempio del JSON che mia arriva:
    codice:
    [
    {"id":"1","titolo":"TITOLO1","gruppo":"GRUPPO1","testo":"TESTO1"},
    {"id":"2","titolo":"TITOLO2","gruppo":"GRUPPO2","testo":"TESTO2"}
    ]
    questa la funzione che sto cercando di usare per la conversione:
    Codice PHP:
    $array json_decode('STRINGA JSON'TRUE);
    $xml arrayToXml($arrayFALSE);
    print_r($xml);

    function 
    arrayToXML($array$xml FALSE) {
        if (
    $xml === false) {
            
    $xml = new SimpleXMLElement('<root/>');
        }

        foreach (
    $array as $key => $value) {
            if (
    is_array($value)) {
                
    arrayToXml($value$xml->addChild($key));
            } else {
                
    $xml->addChild($key$value);
            }
        }
        return 
    $xml->asXML();

    questo l'output che ottengo:
    codice:
    <0>1TITOLO1GRUPPO1TESTO1
    <1>2TITOLO2GRUPPO2TESTO2


    qualche idea???

  2. #2
    ok, questa modifica funziona:
    Codice PHP:
    function arrayToXml($array, &$xml) {
        foreach (
    $array as $key => $value) {
            if (
    is_array($value)) {
                if (!
    is_numeric($key)) {
                    
    $subnode $xml->addChild("$key");
                    
    arrayToXml($value$subnode);
                } else {
                    
    $subnode $xml->addChild("item$key");
                    
    arrayToXml($value$subnode);
                }
            } else {
                
    $xml->addChild("$key"htmlspecialchars("$value"));
            }
        }


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 © 2024 vBulletin Solutions, Inc. All rights reserved.