From ff19323804a48d037ac206722b3de7dd1a7b10a9 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 25 May 2019 15:14:05 -0300 Subject: [PATCH 1/2] method created: processXmlContent() which just returns the PHP array instead of converting it to JSON plain text. Method fromXml() calls processXmlContent() method, fromXml() still returns the result as plain text --- src/Xml2Json.php | 52 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/src/Xml2Json.php b/src/Xml2Json.php index 668ca14..40edce8 100644 --- a/src/Xml2Json.php +++ b/src/Xml2Json.php @@ -50,16 +50,7 @@ class Xml2Json */ public static function fromXml($xmlStringContents, $ignoreXmlAttributes = true) { - // Load the XML formatted string into a Simple XML Element object. - $simpleXmlElementObject = XmlSecurity::scan($xmlStringContents); - - // If it is not a valid XML content, throw an exception. - if (! $simpleXmlElementObject) { - throw new Exception\RuntimeException('Function fromXml was called with invalid XML'); - } - - // Call the recursive function to convert the XML into a PHP array. - $resultArray = static::processXml($simpleXmlElementObject, $ignoreXmlAttributes); + $resultArray = static::processXmlContent($xmlStringContents, $ignoreXmlAttributes); // Convert the PHP array to JSON using Json::encode. return Json::encode($resultArray); @@ -174,4 +165,45 @@ protected static function processXml($simpleXmlElementObject, $ignoreXmlAttribut return [$name => $childArray]; } + + /** + * Converts XML to JSON. + * + * Converts an XML formatted string into a PHP array. + * + * The caller of this function needs to provide only the first parameter, + * which is an XML formatted string. + * + * The second parameter, also optional, allows the user to select if the + * XML attributes in the input XML string should be included or ignored + * during the conversion. + * + * This function converts the XML formatted string into a PHP array via a + * recursive function; it DOES NOT converts that array to json via + * Json::encode(). + * + * NOTE: Encoding native javascript expressions via Zend\Json\Expr is not + * possible. + * + * @param string $xmlStringContents XML String to be converted. + * @param bool $ignoreXmlAttributes Include or exclude XML attributes in + * the conversion process. + * @return array a PHP array. + * @throws Exception\RuntimeException If the input not a XML formatted string. + */ + public static function processXmlContent($xmlStringContents, $ignoreXmlAttributes = true) + { + // Load the XML formatted string into a Simple XML Element object. + $simpleXmlElementObject = XmlSecurity::scan($xmlStringContents); + + // If it is not a valid XML content, throw an exception. + if (! $simpleXmlElementObject) { + throw new Exception\RuntimeException('Function processXmlContent was called with invalid XML'); + } + + // Call the recursive function to convert the XML into a PHP array. + $resultArray = static::processXml($simpleXmlElementObject, $ignoreXmlAttributes); + + return $resultArray; + } } From 3d5e1f733ea3b5c9b9c04dadf07ac8a71eaa8e76 Mon Sep 17 00:00:00 2001 From: Mowses Date: Fri, 21 Jun 2019 22:54:55 -0300 Subject: [PATCH 2/2] convert xml to object instead of array convert xml to object instead of array --- src/Xml2Json.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Xml2Json.php b/src/Xml2Json.php index 40edce8..a16b355 100644 --- a/src/Xml2Json.php +++ b/src/Xml2Json.php @@ -91,14 +91,14 @@ protected static function getXmlValue($simpleXmlElementObject) * The third parameter will be used internally within this function during * the recursive calls. * - * This function converts a SimpleXMLElement object into a PHP array by + * This function converts a SimpleXMLElement object into a PHP object by * calling a recursive function in this class; once all XML elements are * stored to a PHP array, it is returned to the caller. * * @param SimpleXMLElement $simpleXmlElementObject * @param bool $ignoreXmlAttributes * @param int $recursionDepth - * @return array + * @return object * @throws Exception\RecursionException if the XML tree is deeper than the * allowed limit. */ @@ -163,7 +163,7 @@ protected static function processXml($simpleXmlElementObject, $ignoreXmlAttribut $childArray['@text'] = $value; } - return [$name => $childArray]; + return [$name => (object)$childArray]; } /**