From d478552679bc4a418ef5051e562268f75ddf4c19 Mon Sep 17 00:00:00 2001 From: Alessandro Mattiello Date: Tue, 19 May 2015 15:49:03 +0200 Subject: [PATCH 01/18] set a prefix for attributes name. Default prefix = '@' --- LSS/XML2Array.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index 18d4679..f96c685 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -44,7 +44,8 @@ class XML2Array { private static $xml = null; - private static $encoding = 'UTF-8'; + private static $encoding = 'UTF-8'; + private static $prefix_attributes = '@'; /** * Initialize the root XML node [optional] @@ -92,7 +93,7 @@ private static function &convert($node) { switch ($node->nodeType) { case XML_CDATA_SECTION_NODE: - $output['@cdata'] = trim($node->textContent); + $output[static::$prefix_attributes.'cdata'] = trim($node->textContent); break; case XML_TEXT_NODE: @@ -142,7 +143,7 @@ private static function &convert($node) { } // if its an leaf node, store the value in @value instead of directly storing it. if(!is_array($output)) { - $output = array('@value' => $output); + $output = array(static::$prefix_attributes.'value' => $output); } $output['@attributes'] = $a; } From 70c0c93114a838b4713632a3baec664f3d5e0ebf Mon Sep 17 00:00:00 2001 From: Alessandro Mattiello Date: Tue, 19 May 2015 16:31:53 +0200 Subject: [PATCH 02/18] fix --- LSS/XML2Array.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index f96c685..d58c266 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -45,7 +45,7 @@ class XML2Array { private static $xml = null; private static $encoding = 'UTF-8'; - private static $prefix_attributes = '@'; + private static $prefix_attributes = ''; /** * Initialize the root XML node [optional] @@ -145,7 +145,7 @@ private static function &convert($node) { if(!is_array($output)) { $output = array(static::$prefix_attributes.'value' => $output); } - $output['@attributes'] = $a; + $output[static::$prefix_attributes.'attributes'] = $a; } break; } From dda809367ce94ccbd746a9ddac7f481b03f4e80a Mon Sep 17 00:00:00 2001 From: Alessandro Mattiello Date: Mon, 1 Jun 2015 14:07:05 +0200 Subject: [PATCH 03/18] set to '@' default prefix in attribute' --- LSS/XML2Array.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index d58c266..4e3e198 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -45,7 +45,7 @@ class XML2Array { private static $xml = null; private static $encoding = 'UTF-8'; - private static $prefix_attributes = ''; + private static $prefix_attributes = '@'; /** * Initialize the root XML node [optional] From 08ee5a41eed69498a67d5c3e77da64446e88506b Mon Sep 17 00:00:00 2001 From: Herjan Date: Wed, 28 Oct 2015 09:32:03 +0100 Subject: [PATCH 04/18] Code style changes (tabs / spaces fix); Typo fix; --- LSS/Array2XML.php | 86 +++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index 4b91f47..ff532c3 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -2,23 +2,24 @@ /** * OpenLSS - Lighter Smarter Simpler * - * This file is part of OpenLSS. + * This file is part of OpenLSS. * - * OpenLSS is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of - * the License, or (at your option) any later version. + * OpenLSS is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. * - * OpenLSS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. + * OpenLSS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the - * GNU Lesser General Public License along with OpenLSS. - * If not, see . -*/ + * You should have received a copy of the + * GNU Lesser General Public License along with OpenLSS. + * If not, see . + */ namespace LSS; + use \DomDocument; use \Exception; @@ -53,11 +54,10 @@ * $xml = Array2XML::createXML('root_node_name', $php_array); * echo $xml->saveXML(); */ - class Array2XML { private static $xml = null; - private static $encoding = 'UTF-8'; + private static $encoding = 'UTF-8'; /** * Initialize the root XML node [optional] @@ -68,7 +68,7 @@ class Array2XML { public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { self::$xml = new DomDocument($version, $encoding); self::$xml->formatOutput = $format_output; - self::$encoding = $encoding; + self::$encoding = $encoding; } /** @@ -77,7 +77,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu * @param array $arr - aray to be converterd * @return DomDocument */ - public static function &createXML($node_name, $arr=array()) { + public static function &createXML($node_name, $arr = array()) { $xml = self::getXMLRoot(); $xml->appendChild(self::convert($node_name, $arr)); @@ -86,23 +86,29 @@ public static function &createXML($node_name, $arr=array()) { } /** - * Convert an Array to XML - * @param string $node_name - name of the root node to be converted - * @param array $arr - aray to be converterd - * @return DOMNode + * Convert an Array to XML. + * + * @param string $node_name + * Name of the root node to be converted. + * @param array $arr + * Array to be converted. + * + * @throws \Exception + * + * @return \DOMNode */ - private static function &convert($node_name, $arr=array()) { + private static function &convert($node_name, $arr = array()) { //print_arr($node_name); $xml = self::getXMLRoot(); $node = $xml->createElement($node_name); - if(is_array($arr)){ + if (is_array($arr)) { // get the attributes first.; - if(isset($arr['@attributes'])) { - foreach($arr['@attributes'] as $key => $value) { - if(!self::isValidTagName($key)) { - throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); + if (isset($arr['@attributes'])) { + foreach ($arr['@attributes'] as $key => $value) { + if (!self::isValidTagName($key)) { + throw new Exception('[Array2XML] Illegal character in attribute name. attribute: ' . $key . ' in node: ' . $node_name); } $node->setAttribute($key, self::bool2str($value)); } @@ -111,12 +117,12 @@ private static function &convert($node_name, $arr=array()) { // check if it has a value stored in @value, if yes store the value and return // else check if its directly stored as string - if(isset($arr['@value'])) { + if (isset($arr['@value'])) { $node->appendChild($xml->createTextNode(self::bool2str($arr['@value']))); unset($arr['@value']); //remove the key from the array once done. //return from recursion, as a note with value cannot have child nodes. return $node; - } else if(isset($arr['@cdata'])) { + } else if (isset($arr['@cdata'])) { $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); unset($arr['@cdata']); //remove the key from the array once done. //return from recursion, as a note with cdata cannot have child nodes. @@ -125,17 +131,17 @@ private static function &convert($node_name, $arr=array()) { } //create subnodes using recursion - if(is_array($arr)){ + if (is_array($arr)) { // recurse to get the node for that key - foreach($arr as $key=>$value){ - if(!self::isValidTagName($key)) { - throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); + foreach ($arr as $key => $value) { + if (!self::isValidTagName($key)) { + throw new Exception('[Array2XML] Illegal character in tag name. tag: ' . $key . ' in node: ' . $node_name); } - if(is_array($value) && is_numeric(key($value))) { + if (is_array($value) && is_numeric(key($value))) { // MORE THAN ONE NODE OF ITS KIND; // if the new array is numeric index, means it is array of nodes of the same kind // it should follow the parent key name - foreach($value as $k=>$v){ + foreach ($value as $k => $v) { $node->appendChild(self::convert($key, $v)); } } else { @@ -148,7 +154,7 @@ private static function &convert($node_name, $arr=array()) { // after we are done with all the keys in the array (if it is one) // we check if it has any text value, if yes, append it. - if(!is_array($arr)) { + if (!is_array($arr)) { $node->appendChild($xml->createTextNode(self::bool2str($arr))); } @@ -158,8 +164,8 @@ private static function &convert($node_name, $arr=array()) { /* * Get the root XML node, if there isn't one, create it. */ - private static function getXMLRoot(){ - if(empty(self::$xml)) { + private static function getXMLRoot() { + if (empty(self::$xml)) { self::init(); } return self::$xml; @@ -168,7 +174,7 @@ private static function getXMLRoot(){ /* * Get string representation of boolean value */ - private static function bool2str($v){ + private static function bool2str($v) { //convert boolean to text value. $v = $v === true ? 'true' : $v; $v = $v === false ? 'false' : $v; @@ -179,7 +185,7 @@ private static function bool2str($v){ * Check if the tag name or attribute name contains illegal characters * Ref: http://www.w3.org/TR/xml/#sec-common-syn */ - private static function isValidTagName($tag){ + private static function isValidTagName($tag) { $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; } From 7f804fdca86e88d404b139cd948f032da228c81f Mon Sep 17 00:00:00 2001 From: Herjan Date: Wed, 28 Oct 2015 09:35:12 +0100 Subject: [PATCH 05/18] Allow insertion of DOMObjects provided by the array. --- LSS/Array2XML.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index ff532c3..960fc16 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -56,6 +56,9 @@ */ class Array2XML { + /** + * @var DOMDocument + */ private static $xml = null; private static $encoding = 'UTF-8'; @@ -128,6 +131,11 @@ private static function &convert($node_name, $arr = array()) { //return from recursion, as a note with cdata cannot have child nodes. return $node; } + else if (isset($arr['@dom'])) { + $node->appendChild($arr['@dom']); + unset($arr['@dom']); + return $node; + } } //create subnodes using recursion From bb661a1cf43cf510424c51bada87cd7c5bb63570 Mon Sep 17 00:00:00 2001 From: Herjan Date: Wed, 28 Oct 2015 09:53:40 +0100 Subject: [PATCH 06/18] Renamed to @xml; Now using DocumentFragment to insert and XML string as child; --- LSS/Array2XML.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index 960fc16..01d28a1 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -131,8 +131,10 @@ private static function &convert($node_name, $arr = array()) { //return from recursion, as a note with cdata cannot have child nodes. return $node; } - else if (isset($arr['@dom'])) { - $node->appendChild($arr['@dom']); + else if (isset($arr['@xml'])) { + $fragment = $xml->createDocumentFragment(); + $fragment->appendXML($arr['@xml']); + $node->appendChild($fragment); unset($arr['@dom']); return $node; } From a1fa4aa83b9547adb06b6306c6f02d905b7cad29 Mon Sep 17 00:00:00 2001 From: Herjan Date: Wed, 28 Oct 2015 10:36:06 +0100 Subject: [PATCH 07/18] Added documentation for the @xml change --- LSS/Array2XML.php | 2 ++ README.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index 01d28a1..7dc4fe9 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -49,6 +49,8 @@ * - Reverted to version 0.5 * Version: 0.8 (02 May 2012) * - Removed htmlspecialchars() before adding to text node or attributes. + * Version: 0.11 (28 October 2015) + * - Fixed typos; Added support for plain insertion of XML trough @xml. * * Usage: * $xml = Array2XML::createXML('root_node_name', $php_array); diff --git a/README.md b/README.md index 7abe66d..fcebd15 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,38 @@ $array = XML2Array::createArray($xml); print_r($array); ``` +Array2XML +---- + +@xml example: +```php +// Build the array that should be transformed into a XML object. +$array = [ + 'title' => 'A title', + 'body' => [ + '@xml' => '

The content for the news item

', + ], +]; + +// Use the Array2XML object to transform it. +$xml = Array2XML::createXML('news', $array); +echo $xml->saveXML(); +``` +This will result in the following. +```xml + + + A title + + + +

The content for the news item

+ + + +
+``` + Reference ---- More complete references can be found here From 58a84a54fbe08c286a67528c719d643c1256410d Mon Sep 17 00:00:00 2001 From: Herjan Date: Wed, 28 Oct 2015 14:06:57 +0100 Subject: [PATCH 08/18] Unset wrong key, old name for same functionality --- LSS/Array2XML.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index 7dc4fe9..581f313 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -137,7 +137,7 @@ private static function &convert($node_name, $arr = array()) { $fragment = $xml->createDocumentFragment(); $fragment->appendXML($arr['@xml']); $node->appendChild($fragment); - unset($arr['@dom']); + unset($arr['@xml']); return $node; } } From 35b7c636be5118f1676d7f25b49f59b023f0fa2a Mon Sep 17 00:00:00 2001 From: "yitian.ye" Date: Mon, 16 May 2016 15:07:19 +0800 Subject: [PATCH 09/18] Update XML2Array.php - add second parameter to XML2Array::createArray for DOMDocument::load, e.g: LIBXML_NOCDATA - change method visibility from private to protected for overloading --- LSS/XML2Array.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index 4e3e198..cc03efe 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -43,9 +43,9 @@ class XML2Array { - private static $xml = null; - private static $encoding = 'UTF-8'; - private static $prefix_attributes = '@'; + protected static $xml = null; + protected static $encoding = 'UTF-8'; + protected static $prefix_attributes = '@'; /** * Initialize the root XML node [optional] @@ -62,13 +62,14 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu /** * Convert an XML to Array * @param string $node_name - name of the root node to be converted + * @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/zh/libxml.constants.php * @param array $arr - aray to be converterd * @return DOMDocument */ - public static function &createArray($input_xml) { + public static function &createArray($input_xml, $options = 0) { $xml = self::getXMLRoot(); if(is_string($input_xml)) { - $parsed = $xml->loadXML($input_xml); + $parsed = $xml->loadXML($input_xml, $options); if(!$parsed) { throw new Exception('[XML2Array] Error parsing the XML string.'); } @@ -88,7 +89,7 @@ public static function &createArray($input_xml) { * @param mixed $node - XML as a string or as an object of DOMDocument * @return mixed */ - private static function &convert($node) { + protected static function &convert($node) { $output = array(); switch ($node->nodeType) { @@ -155,7 +156,7 @@ private static function &convert($node) { /* * Get the root XML node, if there isn't one, create it. */ - private static function getXMLRoot(){ + protected static function getXMLRoot(){ if(empty(self::$xml)) { self::init(); } From 9a033906d404f616895db9920403dfeec3fb9e00 Mon Sep 17 00:00:00 2001 From: Bryan Tong Date: Wed, 2 Nov 2016 12:55:00 -0600 Subject: [PATCH 10/18] Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index fcebd15..2f9e168 100644 --- a/README.md +++ b/README.md @@ -53,3 +53,14 @@ More complete references can be found here http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array/ http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes/ +## Changelog + +### 0.5.0 +* add second parameter to XML2Array::createArray for DOMDocument::load, e.g: LIBXML_NOCDATA +* change method visibility from private to protected for overloading +* Merge pull request #5 to add child xml +* Merge pull request #4 to change method visibility and add second parameter for load. + + +### 0.1.0 +* Initial Release From 2aff5f37b2c0b5b019b0f644a7ebd816cb6d5cc6 Mon Sep 17 00:00:00 2001 From: Andrei Sandulescu Date: Wed, 9 Nov 2016 11:39:12 +0200 Subject: [PATCH 11/18] - avoid fatal error if the content looks like 'You are being redirected.' --- LSS/XML2Array.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index cc03efe..31ce04b 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -110,6 +110,10 @@ protected static function &convert($node) { if(isset($child->tagName)) { $t = $child->tagName; + // avoid fatal error if the content looks like 'You are being redirected.' + if(isset($output) && !is_array($output)) { + continue; + } // assume more nodes of same kind are coming if(!isset($output[$t])) { $output[$t] = array(); From c8b5998a342d7861f2e921403f44e0a2f3ef2be0 Mon Sep 17 00:00:00 2001 From: Bryan Tong Date: Thu, 10 Nov 2016 12:10:18 -0700 Subject: [PATCH 12/18] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 2f9e168..50b6293 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ More complete references can be found here ## Changelog +### 0.5.1 +* Fix fata error when the array passed is empty fixed by pull request #6 + ### 0.5.0 * add second parameter to XML2Array::createArray for DOMDocument::load, e.g: LIBXML_NOCDATA * change method visibility from private to protected for overloading From 7821d82af544351d581c098631e07cd9962c7aab Mon Sep 17 00:00:00 2001 From: Andrei Sandulescu Date: Mon, 29 May 2017 15:26:55 +0300 Subject: [PATCH 13/18] - suppress errors (to avoid warnings) if an invalid xml is passed --- LSS/XML2Array.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index 31ce04b..200a46c 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -69,7 +69,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu public static function &createArray($input_xml, $options = 0) { $xml = self::getXMLRoot(); if(is_string($input_xml)) { - $parsed = $xml->loadXML($input_xml, $options); + $parsed = @$xml->loadXML($input_xml, $options); if(!$parsed) { throw new Exception('[XML2Array] Error parsing the XML string.'); } From 363496d3dfefbfc9e7a47418bdbe09cc4f0e9869 Mon Sep 17 00:00:00 2001 From: Kuang Jiaye Date: Sun, 13 Aug 2017 16:47:37 +0800 Subject: [PATCH 14/18] Update XML2Array.php fix the mistake of createArray's return data type to advoid warnings --- LSS/XML2Array.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index 31ce04b..8ae8593 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -64,7 +64,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu * @param string $node_name - name of the root node to be converted * @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/zh/libxml.constants.php * @param array $arr - aray to be converterd - * @return DOMDocument + * @return array */ public static function &createArray($input_xml, $options = 0) { $xml = self::getXMLRoot(); From 410f6c4ca7d7c65719fe6d61114109de430e4b53 Mon Sep 17 00:00:00 2001 From: Max Kovpak Date: Wed, 10 Oct 2018 14:32:49 +0300 Subject: [PATCH 15/18] Changed lang to default --- LSS/XML2Array.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index 31ce04b..043233b 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -62,7 +62,7 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu /** * Convert an XML to Array * @param string $node_name - name of the root node to be converted - * @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/zh/libxml.constants.php + * @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/libxml.constants.php * @param array $arr - aray to be converterd * @return DOMDocument */ From 787fd639bc65820fb525dce94f55bd2569d3629a Mon Sep 17 00:00:00 2001 From: Ryan P Date: Tue, 16 Oct 2018 12:19:06 -0400 Subject: [PATCH 16/18] XML comments Add ability to post XML comments via a '@comment' attribute in the array (string only) --- LSS/Array2XML.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index 581f313..99e0659 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -133,6 +133,10 @@ private static function &convert($node_name, $arr = array()) { //return from recursion, as a note with cdata cannot have child nodes. return $node; } + else if (isset($arr['@comment']) && is_string($arr['@comment'])) { + $node->appendChild($xml->createComment(self::bool2str($arr['@comment']))); + unset($arr['@comment']); + } else if (isset($arr['@xml'])) { $fragment = $xml->createDocumentFragment(); $fragment->appendXML($arr['@xml']); From 9d2ab616d532524a982f48f4570fb6fd58b9a688 Mon Sep 17 00:00:00 2001 From: averichev Date: Wed, 5 Dec 2018 17:59:27 +0300 Subject: [PATCH 17/18] Added callback function For example for progressbar --- LSS/XML2Array.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index e2c8b97..ad5dafa 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -64,12 +64,13 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu * @param string $node_name - name of the root node to be converted * @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/libxml.constants.php * @param array $arr - aray to be converterd + * @param mixed $callback - callback function * @return array */ - public static function &createArray($input_xml, $options = 0) { + public static function &createArray($input_xml, $options = 0, $callback = null) { $xml = self::getXMLRoot(); if(is_string($input_xml)) { - $parsed = @$xml->loadXML($input_xml, $options); + $parsed = $xml->loadXML($input_xml, $options); if(!$parsed) { throw new Exception('[XML2Array] Error parsing the XML string.'); } @@ -79,7 +80,7 @@ public static function &createArray($input_xml, $options = 0) { } $xml = self::$xml = $input_xml; } - $array[$xml->documentElement->tagName] = self::convert($xml->documentElement); + $array[$xml->documentElement->tagName] = self::convert($xml->documentElement, $callback); self::$xml = null; // clear the xml node in the class for 2nd time use. return $array; } @@ -87,9 +88,10 @@ public static function &createArray($input_xml, $options = 0) { /** * Convert an Array to XML * @param mixed $node - XML as a string or as an object of DOMDocument + * @param mixed $callback - callback function * @return mixed */ - protected static function &convert($node) { + protected static function &convert($node, $callback = null) { $output = array(); switch ($node->nodeType) { @@ -102,9 +104,11 @@ protected static function &convert($node) { break; case XML_ELEMENT_NODE: - // for each child node, call the covert function recursively for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { + if ($callback!==null) { + $callback($m=$node->childNodes->length, $i); + } $child = $node->childNodes->item($i); $v = self::convert($child); if(isset($child->tagName)) { From a91f18a8dfc69ffabe5f9b068bc39bb202c81d90 Mon Sep 17 00:00:00 2001 From: Nullivex Date: Fri, 29 Mar 2019 14:06:56 -0600 Subject: [PATCH 18/18] Update to release 1.0.0 --- .gitignore | 1 + README.md | 3 +++ composer.json | 8 ++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index de4a392..1ee4bf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.idea /vendor /composer.lock diff --git a/README.md b/README.md index 50b6293..d3207ce 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ More complete references can be found here ## Changelog +### 1.0.0 +* Add ability for callbacks during processing to check status. + ### 0.5.1 * Fix fata error when the array passed is empty fixed by pull request #6 diff --git a/composer.json b/composer.json index 0a514c9..7471231 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "openlss/lib-array2xml" - ,"homepage": "http://openlss.org" + ,"homepage": "https://www.nullivex.com" ,"description": "Array2XML conversion library credit to lalit.org" ,"license": "Apache-2.0" ,"type": "library" @@ -13,13 +13,13 @@ ,"authors": [ { "name": "Bryan Tong" - ,"email": "contact@nullivex.com" - ,"homepage": "http://bryantong.com" + ,"email": "bryan@nullivex.com" + ,"homepage": "https://www.nullivex.com" } ,{ "name": "Tony Butler" ,"email": "spudz76@gmail.com" - ,"homepage": "http://openlss.org" + ,"homepage": "https://www.nullivex.com" } ] ,"require": {