public function __construct($xml) { $json = Json::fromXml($xml); $array = Json::decode($json, true); $this->code = $array['code']; $this->reference = $array['reference']; $this->type = $array['type']; $this->status = $array['status']; $this->cancellationSource = $array['cancellationSource']; }
/** * Deserialize XML to PHP value * * @param string $xml * @return mixed * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function unserialize($xml) { try { $json = Json::fromXml($xml); $unserialized = Json::decode($json, Json::TYPE_OBJECT); } catch (\InvalidArgumentException $e) { throw new Exception\InvalidArgumentException('Unserialization failed: ' . $e->getMessage(), 0, $e); } catch (\Exception $e) { throw new Exception\RuntimeException('Unserialization failed: ' . $e->getMessage(), 0, $e); } return $unserialized; }
/** * @group ZF-11385 * @dataProvider providerNestingDepthIsHandledProperly */ public function testNestingDepthIsHandledProperlyWhenNestingDepthDoesNotExceedMaximum($xmlStringContents) { try { Json\Json::$maxRecursionDepthAllowed = 25; $jsonString = Json\Json::fromXml($xmlStringContents, true); $jsonArray = Json\Json::decode($jsonString, Json\Json::TYPE_ARRAY); $this->assertNotNull($jsonArray, "JSON decode result is NULL"); $this->assertSame('A', $jsonArray['response']['message_type']['defaults']['close_rules']['after_responses']); } catch (Zend\Json\Exception\RecursionException $ex) { $this->fail('Zend_Json::fromXml does not implement recursion check properly'); } }
/** * @group ZF-11385 * @dataProvider providerNestingDepthIsHandledProperly */ public function testNestingDepthIsHandledProperlyWhenNestingDepthDoesNotExceedMaximum($xmlStringContents) { Json\Json::$maxRecursionDepthAllowed = 25; $jsonString = Json\Json::fromXml($xmlStringContents, true); $jsonArray = Json\Json::decode($jsonString, Json\Json::TYPE_ARRAY); $this->assertNotNull($jsonArray, "JSON decode result is NULL"); $this->assertSame('A', $jsonArray['response']['message_type']['defaults']['close_rules']['after_responses']); }
protected function parseXmlResponse(Response $response) { $responseText = $response->getBody(); if (!$responseText) { return; } $data = Json::decode(Json::fromXml($responseText), Json::TYPE_ARRAY); return $data; }
/** * 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 = <<<EOT <?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[ /* It may not be a syntactically valid PHP code. It is used here just to illustrate the CDATA feature of Zend_Xml2JSON */ <?php include 'example.php'; new SimpleXMLElement(); echo(getMovies()->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(); ?> ]]> </code> </listing> </demo> 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 = ""; // Convert XML to JSON now. // fromXml function simply takes a String containing XML contents as input. $jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes); // Convert the JSON string into a PHP array. $phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY); // 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"); }