to_xml ( $json, $parser )

Converts a JSON string to a CFSimpleXML object.

Access

public static

Parameters

Parameter

Type

Required

Description

$json

string
array

Required

Pass either a valid JSON-formatted string, or an associative array.

$parser

string

Optional

The name of the class to use to parse the XML. This class should extend SimpleXMLElement. Has a default value of CFSimpleXML.

Returns

Type

Description

CFSimpleXML

An XML representation of the data.

Source

Method defined in utilities/json.class.php | Toggle source view (45 lines) | View on GitHub

public static function to_xml($json, $parser = 'CFSimpleXML')
{
    // If we haven't parsed the JSON, do it
    if (!is_array($json))
    {
        // Handle the case of JSON-encoded NULL value
        if ($json === 'null')
        {
            $json = null;
        }
        else
        {
            $json = json_decode($json, true);

            if (function_exists('json_last_error'))
            {
                // Did we encounter an error?
                switch (json_last_error())
                {
                    case JSON_ERROR_DEPTH:
                        throw new JSON_Exception('Maximum stack depth exceeded.');

                    case JSON_ERROR_CTRL_CHAR:
                        throw new JSON_Exception('Unexpected control character found.');

                    case JSON_ERROR_SYNTAX:
                        throw new JSON_Exception('Syntax error; Malformed JSON.');

                    case JSON_ERROR_STATE_MISMATCH:
                        throw new JSON_Exception('Invalid or malformed JSON.');
                }
            }
            // json_last_error() not available?
            elseif ($json === null)
            {
                throw new JSON_Exception('Unknown JSON error. Be sure to validate your JSON and read the notes on http://php.net/json_decode.');
            }
        }
    }

    // Hand off for the recursive work
    $string = Transmogrifier::to_xml($json, 'rootElement');

    return simplexml_load_string($string, $parser, LIBXML_NOCDATA);
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback