Skip to content

Commit f3813a3

Browse files
authored
Merge pull request #13 from averichev/patch-2
Added callback function
2 parents f3a5bca + 9d2ab61 commit f3813a3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

LSS/XML2Array.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu
6464
* @param string $node_name - name of the root node to be converted
6565
* @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/libxml.constants.php
6666
* @param array $arr - aray to be converterd
67+
* @param mixed $callback - callback function
6768
* @return array
6869
*/
69-
public static function &createArray($input_xml, $options = 0) {
70+
public static function &createArray($input_xml, $options = 0, $callback = null) {
7071
$xml = self::getXMLRoot();
7172
if(is_string($input_xml)) {
72-
$parsed = @$xml->loadXML($input_xml, $options);
73+
$parsed = $xml->loadXML($input_xml, $options);
7374
if(!$parsed) {
7475
throw new Exception('[XML2Array] Error parsing the XML string.');
7576
}
@@ -79,17 +80,18 @@ public static function &createArray($input_xml, $options = 0) {
7980
}
8081
$xml = self::$xml = $input_xml;
8182
}
82-
$array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
83+
$array[$xml->documentElement->tagName] = self::convert($xml->documentElement, $callback);
8384
self::$xml = null; // clear the xml node in the class for 2nd time use.
8485
return $array;
8586
}
8687

8788
/**
8889
* Convert an Array to XML
8990
* @param mixed $node - XML as a string or as an object of DOMDocument
91+
* @param mixed $callback - callback function
9092
* @return mixed
9193
*/
92-
protected static function &convert($node) {
94+
protected static function &convert($node, $callback = null) {
9395
$output = array();
9496

9597
switch ($node->nodeType) {
@@ -102,9 +104,11 @@ protected static function &convert($node) {
102104
break;
103105

104106
case XML_ELEMENT_NODE:
105-
106107
// for each child node, call the covert function recursively
107108
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
109+
if ($callback!==null) {
110+
$callback($m=$node->childNodes->length, $i);
111+
}
108112
$child = $node->childNodes->item($i);
109113
$v = self::convert($child);
110114
if(isset($child->tagName)) {

0 commit comments

Comments
 (0)