Введение в Zend Framework

 Learning Zend Framework


 Zend Framework Reference

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Ttranslation 27.4% Update 2010-11-28 - Revision 23238 - Version ZF 1.11.x

    39.4. XML to JSON conversion

    Zend_Json provides a convenience method for transforming XML formatted data into JSON format. This feature was inspired from an IBM developerWorks article.

    Zend_Json includes a static function called Zend_Json::fromXml(). This function will generate JSON from a given XML input. This function takes any arbitrary XML string as an input parameter. It also takes an optional boolean input parameter to instruct the conversion logic to ignore or not ignore the XML attributes during the conversion process. If this optional input parameter is not given, then the default behavior is to ignore the XML attributes. This function call is made as shown below:

    // fromXml function simply takes a String containing XML contents
    // as input.
    $jsonContents Zend_Json::fromXml($xmlStringContentstrue);

    Zend_Json::fromXml() function does the conversion of the XML formatted string input parameter and returns the equivalent JSON formatted string output. In case of any XML input format error or conversion logic error, this function will throw an exception. The conversion logic also uses recursive techniques to traverse the XML tree. It supports recursion upto 25 levels deep. Beyond that depth, it will throw a Zend_Json_Exception. There are several XML files with varying degree of complexity provided in the tests directory of Zend Framework. They can be used to test the functionality of the xml2json feature.

    The following is a simple example that shows both the XML input string passed to and the JSON output string returned as a result from the Zend_Json::fromXml() function. This example used the optional function parameter as not to ignore the XML attributes during the conversion. Hence, you can notice that the resulting JSON string includes a representation of the XML attributes present in the XML input string.

    XML input string passed to Zend_Json::fromXml() function:

    <?xml version="1.0" encoding="UTF-8"?>
        <book id="1">
            <title>Code Generation in Action</title>

        <book id="2">
            <title>PHP Hacks</title>

        <book id="3">
            <title>Podcasting Hacks</title>

    JSON output string returned from Zend_Json::fromXml() function:

    "books" : {
    "book" : [ {
    "@attributes" : {
    "id" "1"
    "title" "Code Generation in Action",
    "author" : {
    "first" "Jack""last" "Herrington"
    "publisher" "Manning"
    }, {
    "@attributes" : {
    "id" "2"
    "title" "PHP Hacks""author" : {
    "first" "Jack""last" "Herrington"
    "publisher" "O'Reilly"
    }, {
    "@attributes" : {
    "id" "3"
    "title" "Podcasting Hacks""author" : {
    "first" "Jack""last" "Herrington"
    "publisher" "O'Reilly"

    More details about this xml2json feature can be found in the original proposal itself. Take a look at the Zend_xml2json proposal.

    digg delicious meneame google twitter technorati facebook