John Doe 123-456-7890 Jane Doe 123-456-0000 John Smith 123-456-1111 Jane Smith 123-456-9999 EOT; // There are not going to be any XML attributes in this test XML. // Hence, set the flag to ignore XML attributes. $ignoreXmlAttributes = true; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception."); // Convert the JSON string into a PHP array. $phpArray = Zend_Json::decode($jsonContents); // Test if it is not a NULL object. $this->assertNotNull($phpArray, "JSON result for XML input 1 is NULL"); // Test for one of the expected fields in the JSON result. $this->assertSame("Jane Smith", $phpArray['contacts']['contact'][3]['name'], "The last contact name converted from XML input 1 is not correct"); } // End of function testUsingXML1 /** * xml2json Test 2 * It tests the conversion of book publication xml into Json format. * * XML characteristic to be tested: XML containing an array of child elements with XML attributes. * */ public function testUsingXML2() { // Set the XML contents that will be tested here. $xmlStringContents = << Code Generation in Action JackHerrington Manning PHP Hacks JackHerrington O'Reilly Podcasting Hacks JackHerrington O'Reilly EOT; // There are going to be XML attributes in this test XML. // Hence, set the flag NOT to ignore XML attributes. $ignoreXmlAttributes = false; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception."); // Convert the JSON string into a PHP array. $phpArray = Zend_Json::decode($jsonContents); // Test if it is not a NULL object. $this->assertNotNull($phpArray, "JSON result for XML input 2 is NULL"); // Test for one of the expected fields in the JSON result. $this->assertSame("Podcasting Hacks", $phpArray['books']['book'][2]['title'], "The last book title converted from XML input 2 is not correct"); // Test one of the expected XML attributes carried over in the JSON result. $this->assertSame("3", $phpArray['books']['book'][2]['@attributes']['id'], "The last id attribute converted from XML input 2 is not correct"); } // End of function testUsingXML2 /** * xml2json Test 3 * It tests the conversion of food menu xml into Json format. * * XML characteristic to be tested: XML containing an array of child elements. * */ public function testUsingXML3() { // Set the XML contents that will be tested here. $xmlStringContents = << Belgian Waffles $5.95 two of our famous Belgian Waffles with plenty of real maple syrup 650 Strawberry Belgian Waffles $7.95 light Belgian waffles covered with strawberries and whipped cream 900 Berry-Berry Belgian Waffles $8.95 light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 French Toast $4.50 thick slices made from our homemade sourdough bread 600 Homestyle Breakfast $6.95 two eggs, bacon or sausage, toast, and our ever-popular hash browns 950 EOT; // There are not going to be any XML attributes in this test XML. // Hence, set the flag to ignore XML attributes. $ignoreXmlAttributes = true; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception."); // Convert the JSON string into a PHP array. $phpArray = Zend_Json::decode($jsonContents); // Test if it is not a NULL object. $this->assertNotNull($phpArray, "JSON result for XML input 3 is NULL"); // Test for one of the expected fields in the JSON result. $this->assertContains("Homestyle Breakfast", $phpArray['breakfast_menu']['food'][4], "The last breakfast item name converted from XML input 3 is not correct"); } // End of function testUsingXML3 /** * xml2json Test 4 * It tests the conversion of RosettaNet purchase order xml into Json format. * * XML characteristic to be tested: XML containing an array of child elements and multiple attributes. * */ public function testUsingXML4() { // Set the XML contents that will be tested here. $xmlStringContents = << John Doe john@nodomain.net 1-123-456-7890 Electronic Component 25 microfarad 16 volt surface-mount tantalum capacitor 42 CAPACITOR - FIXED - TANTAL - SOLID Specific Features R Body Material C Terminal Position A Package: Outline Style CP Lead Form D Rated Capacitance 0.000025 Tolerance On Rated Capacitance (%) 10 Leakage Current (Short Term) 0.0000001 Rated Voltage 16 Operating Temperature 140 -10 Mounting Surface Capacitors 'R' Us, Inc. 98-765-4321 http://sylviaearle/capaciorsRus/wsdl/buyerseller-implementation.wsdl EOT; // There are going to be XML attributes in this test XML. // Hence, set the flag NOT to ignore XML attributes. $ignoreXmlAttributes = false; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception."); // Convert the JSON string into a PHP array. $phpArray = Zend_Json::decode($jsonContents); // Test if it is not a NULL object. $this->assertNotNull($phpArray, "JSON result for XML input 4 is NULL"); // Test for one of the expected fields in the JSON result. $this->assertContains("98-765-4321", $phpArray['PurchaseRequisition']['Item']['Vendor'], "The vendor id converted from XML input 4 is not correct"); // Test for the presence of multiple XML attributes present that were carried over in the JSON result. $this->assertContains("UNSPSC", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The type attribute converted from XML input 4 is not correct"); $this->assertContains("32121501", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The value attribute converted from XML input 4 is not correct"); $this->assertContains("Fixed capacitors", $phpArray['PurchaseRequisition']['Item']['Specification']['Category']['@attributes'], "The name attribute converted from XML input 4 is not correct"); } // End of function testUsingXML4 /** * xml2json Test 5 * It tests the conversion of TV shows xml into Json format. * * XML characteristic to be tested: XML containing simple CDATA. * */ public function testUsingXML5() { // Set the XML contents that will be tested here. $xmlStringContents = << The Simpsons EOT; // There are not going to be any XML attributes in this test XML. // Hence, set the flag to ignore XML attributes. $ignoreXmlAttributes = true; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception."); // Convert the JSON string into a PHP array. $phpArray = Zend_Json::decode($jsonContents); // Test if it is not a NULL object. $this->assertNotNull($phpArray, "JSON result for XML input 5 is NULL"); // Test for one of the expected CDATA fields in the JSON result. $this->assertContains("Lois & Clark", $phpArray['tvshows']['show'][1]['name'], "The CDATA name converted from XML input 5 is not correct"); } // End of function testUsingXML5 /** * xml2json Test 6 * It tests the conversion of demo application xml into Json format. * * XML characteristic to be tested: XML containing a large CDATA. * */ public function testUsingXML6() { // Set the XML contents that will be tested here. $xmlStringContents = << Killer Demo John Doe LAMP Zend PHP movie[0]->characters->addChild('character')); getMovies()->movie[0]->characters->character->addChild('name', "Mr. Parser"); getMovies()->movie[0]->characters->character->addChild('actor', "John Doe"); // Add it as a child element. getMovies()->movie[0]->addChild('rating', 'PG'); getMovies()->movie[0]->rating->addAttribute("type", 'mpaa'); echo getMovies()->asXML(); ?> ]]> EOT; // There are not going to be any XML attributes in this test XML. // Hence, set the flag to ignore XML attributes. $ignoreXmlAttributes = true; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertSame($ex, null, "Zend_JSON::fromXml returned an exception."); // Convert the JSON string into a PHP array. $phpArray = Zend_Json::decode($jsonContents); // Test if it is not a NULL object. $this->assertNotNull($phpArray, "JSON result for XML input 6 is NULL"); // Test for one of the expected fields in the JSON result. $this->assertContains("Zend", $phpArray['demo']['framework']['name'], "The framework name field converted from XML input 6 is not correct"); // Test for one of the expected CDATA fields in the JSON result. $this->assertContains('echo getMovies()->asXML();', $phpArray['demo']['listing']['code'], "The CDATA code converted from XML input 6 is not correct"); } // End of function testUsingXML6 /** * xml2json Test 7 * It tests the conversion of an invalid xml into Json format. * * XML characteristic to be tested: XML containing invalid syntax. * */ /* public function testUsingXML7() { // Set the XML contents that will be tested here. $xmlStringContents = << EOT; // There are not going to be any XML attributes in this test XML. // Hence, set the flag to ignore XML attributes. $ignoreXmlAttributes = true; $jsonContents = ""; $ex = null; // Convert XNL to JSON now. // fromXml function simply takes a String containing XML contents as input. try { $jsonContents = Zend_Json::fromXml($xmlStringContents, $ignoreXmlAttributes); } catch (Exception $ex) { ; } $this->assertNotSame($ex, null, "Zend_JSON::fromXml returned an exception."); } // End of function testUsingXML7 */ } // End of class Zend_Json_JsonXMLTest