Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current  |   View Page History

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[

Zend Framework: Zend_xml2json Component Proposal

Proposed Component Name Zend_xml2json
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend_xml2json
Proposers Senthil Nathan
Revision 0.1 - 23 April 2007: Initial proposal.
0.2 - 30 May 2007: Revised proposal after incorporating the review comments. (wiki revision: 26)

Table of Contents

1. Overview

This is a proposal to add a new feature to the Zend_Json module. This new feature will provide a conversion capability. Specifically, it will allow any arbitrary XML formatted document to be converted into JSON formatted string. The following paragraph forms the motivation for this proposal.

Since the late 1990s, many enterprise data architectures have been using XML. That choice made it a common practice for mid-tier (PHP and J2EE) servers to exchange enterprise data with the browser applications in XML format. However, current popularity of JSON makes it a credible data interchange format between browser applications and mid-tier servers. Combined with the rising adoption of Ajax in enterprise-grade applications, JSON is emerging as a natural choice for data encoding at both the client and middle tiers. The key reason being that there is no extra parsing required in the browser applications to interpret JSON formatted data. Because, JSON is readily consumable in browser applications as it follows the data syntax rules of JavaScript. In addition, JSON makes the job of the browser application developers a lot simpler by eliminating the need to deal with all the intricacies of XML.

As Web 2.0 technologies proliferate in large enterprises, it will become necessary for PHP-based mid-tier server applications to exchange enterprise application data with the browser applications in JSON format rather than in XML format. In order to achieve this, server-side PHP application developers have to convert XML-formatted data into JSON-formatted data before sending it to browser. In reality, it would be extremely beneficial to make this conversion as part of the Zend Framework. Hence, this proposal is made to add this simple, but powerful feature in the Zend_Json module.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will require a new static function to be added to Json.php
  • This component will not require use of configuration files.
  • This component will add a new class to Zend_Json module.
  • Given a XML formatted string as input, this component will convert the entire XML content into JSON formatted string.
  • This component will keep the nested structure of the XML content intact in the resulting JSON data as well.
  • This component will also optionally convert the XML attributes present in the XML content into a corresponding representation in the resulting JSON data.

4. Dependencies on Other Framework Components

  • Zend_Json
  • Zend_Exception

5. Theory of Operation

In order to bring the xml2json conversion feature to the Zend Framework, we will just add a new static function to the Zend_Json class in Json.php file. That function is described below.

  • fromXML

A function named "fromXML" is the one that users will call to convert any arbitrary XML content to JSON data. This function will take as input, a string containing XML formatted content and a boolean value of true to indicate the need to ignore or false for not to ignore the XML attributes in the conversion process. This function will then convert this input XML string value into SimpleXML. This will result in the SimpleXML object getting converted into a PHP array object. In the end, a call will be made to the encode function in the Zend_Json class to convert the PHP array object to JSON formatted string. The conversion result will be returned to the caller as a JSON formatted string.

By default, this function will ignore the XML attributes i.e. it will not do anything with the XML attributes during the conversion process. It recursively traverses the nested XML content structure and stores the visited XML contents into a PHP associative array structure. At the end of the XML tree traversal, it returns the PHP array with all the element/value pairs that were present in the XML input content. If needed, this function will also optionally store the XML attributes in the PHP array so that it can be represented in the final JSON data.

6. Milestones / Tasks

  • Milestone 1: [DONE] Publish this proposal.
  • Milestone 2: Revise proposal, approve for Incubator development.
  • Milestone 3: Commit working prototype to Incubator.
  • Milestone 4: Commit unit tests and documentation.
  • Milestone 5: Promote code from incubator to framework release.

7. Class Index

  • Zend_Json

8. Use Cases

The use cases below show a few scenarios that explain the input and output characteristics of Zend_xml2json function.

UC-01

The XML content shown above defines a parent element <contacts> that can include one or more <contact> child element. Every <contact> child element will have a nested structure of its own children. When this XML content is passed as an input to the xml2json conversion function, the expected output in JSON format is as shown below.

As you can see from the JSON formatted output shown above, each piece of element information found in the XML input content is preserved in the JSON formatted output content resulting from xml2json conversion. Optionally, xml2json function can be made to carry over the XML attributes into the JSON output.

UC-02

XML input content shown above contains a parent element <books> which in turn has repeating <book> child elements. The <book> element contains an XML attribute to define an id for that book. Each <book> element also contains other child elements. This XML is moderately complex as compared to the one we saw in Use Case 1. The JSON formatted text shown below is the result/output generated by the xml2json conversion function.

The JSON representation shown above preserves all the content structure and details as in the XML input. The repeating <book> elements in XML are converted into a JSON array of book objects. If the conversion function is called with an option to retain the XML attributes, every JSON book object will carry the attributes in a @attributes property.

UC-03

XML input content shown above represents an example of a purchase order that follows the RosettaNet industry standard. It is sufficiently complex in terms of structure, nesting, empty elements, multiple XML attributes etc. This XML input content, when used as input to the xml2json function will produce the resulting JSON formatted text as shown below.

As we observed in use cases 1 and 2, the above-mentioned JSON formatted text result preserves the content structure as laid out in the XML input. Note that the empty element <Billing /> has been retained as a JSON property with a value of an empty string. Multiple XML attributes present in the <Category> element have been carried over in the corresponding JSON data. All the repeating child elements of <query> also resulted in a JSON array of element objects.

All three use cases combined together demonstrate the potential use of the xml2json conversion function named fromXML. Even though this function looks deceptively simple, the benefit it will deliver to the PHP user community could be very valuable.

9. Class Skeletons

As explained earlier, a new static function will be added to the Zend_Json class in Json.php file. It is a recursive function that contains all the logic required for the XML to JSON conversion. This static function is shown below.

The following is a test driver that can be used to exercise the xml2json feature in the Zend framework. This PHP file (Zend_xml2json_test.php) can be run with a command-line argument of any valid filename that contains XML contents.

]]></ac:plain-text-body></ac:macro>

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.