compared with
Version 34 by Bill Karwin
on Jun 06, 2007 16:31.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (4)

View Page History
{code}

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.
||UC-04||

{code}
<?xml version="1.0"?>
<tvshows>
<show>
<name>The Simpsons</name>
</show>

<show>
<name><![CDATA[Lois & Clark]]></name>
</show>
</tvshows>
{code}

The XML content shown above defines a simple tree of different TV shows with repeating elements. One thing that stands out in this XML content is the CDATA section, which can contain any arbitrary general character data. CDATA is useful to express XML fragment as text data within an XML document. In the XML content above, CDATA section contains only a string with an ampersand character which is a special character in XML. When this XML content is passed as an input to the xml2json conversion function, the expected output in JSON format is as shown below.

{code}
{
"tvshows" : {
"show":
[
{"name":"The Simpsons"},
{"name":"Lois & Clark"}
]
}
}
{code}

As you can see from the JSON formatted output shown above, the text data inside of the CDATA section is preserved in the conversion. It is taken care of by the conversion logic.

||UC-05||

{code}
<?xml version="1.0"?>
<demo>
<application>
<name>Killer Demo</name>
</application>

<author>
<name>John Doe</name>
</author>

<platform>
<name>LAMP</name>
</platform>

<framework>
<name>Zend</name>
</framework>

<language>
<name>PHP</name>
</language>

<listing>
<code>
<![CDATA[
<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

$character = $xml->movie[0]->characters->addChild('character');
$character->addChild('name', "Mr. Parser");
$character->addChild('actor', "John Doe");
// Add it as a child element.
$rating = $xml->movie[0]->addChild('rating', 'PG');
$rating->addAttribute("type", 'mpaa');

echo $xml->asXML();
?>
]]>
</code>
</listing>
</demo>

{code}

The XML content shown above defines a simple hierarchy of elements that define a structure for a demo application. The interesting part comes at the end of the document where the CDATA section is specified. The CDATA section includes a complete listing of a PHP script. It is important to note that the contents inside of this CDATA section include several reserved characters such as double quote, greater than character, forward slashes etc. This is a good test case for the xml2json conversion function. When this XML content is passed as an input to the xml2json conversion function, the expected output in JSON format is as shown below.

{code}
{
"demo":{
"application":
{"name":"Killer Demo"},
"author":
{"name":"John Doe"},
"platform":
{"name":"LAMP"},
"framework":
{"name":"Zend"},
"language":
{"name":"PHP"},
"listing":
{"code":
"<?php\n
include 'example.php';\n
$xml = new SimpleXMLElement($xmlstr);\n\n
$character = $xml->movie[0]->characters->addChild('character');\n
$character->addChild('name', \"Mr. Parser\");\n
$character->addChild('actor', \"John Doe\");\n\n
\/\/ Add it as a child element.\n
$rating = $xml->movie[0]->addChild('rating', 'PG');\n
$rating->addAttribute(\"type\", 'mpaa');\n\n
echo $xml->asXML();\n
?>"
}
}
}
{code}

As you can see from the JSON formatted output shown above, the conversion logic preserved the entire text (PHP script in this case) that was part of the CDATA section in the input XML. It is also interesting to note that the entire CDATA section content is converted into a single string. Some of the special characters are automatically escaped by preceding such characters with a backslash character. This powerful feature to handle CDATA sections is built into the xml2json conversion logic.

All five 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.

{zone-data}