@@ -64,12 +64,13 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu
64
64
* @param string $node_name - name of the root node to be converted
65
65
* @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/libxml.constants.php
66
66
* @param array $arr - aray to be converterd
67
+ * @param mixed $callback - callback function
67
68
* @return array
68
69
*/
69
- public static function &createArray ($ input_xml , $ options = 0 ) {
70
+ public static function &createArray ($ input_xml , $ options = 0 , $ callback = null ) {
70
71
$ xml = self ::getXMLRoot ();
71
72
if (is_string ($ input_xml )) {
72
- $ parsed = @ $ xml ->loadXML ($ input_xml , $ options );
73
+ $ parsed = $ xml ->loadXML ($ input_xml , $ options );
73
74
if (!$ parsed ) {
74
75
throw new Exception ('[XML2Array] Error parsing the XML string. ' );
75
76
}
@@ -79,17 +80,18 @@ public static function &createArray($input_xml, $options = 0) {
79
80
}
80
81
$ xml = self ::$ xml = $ input_xml ;
81
82
}
82
- $ array [$ xml ->documentElement ->tagName ] = self ::convert ($ xml ->documentElement );
83
+ $ array [$ xml ->documentElement ->tagName ] = self ::convert ($ xml ->documentElement , $ callback );
83
84
self ::$ xml = null ; // clear the xml node in the class for 2nd time use.
84
85
return $ array ;
85
86
}
86
87
87
88
/**
88
89
* Convert an Array to XML
89
90
* @param mixed $node - XML as a string or as an object of DOMDocument
91
+ * @param mixed $callback - callback function
90
92
* @return mixed
91
93
*/
92
- protected static function &convert ($ node ) {
94
+ protected static function &convert ($ node, $ callback = null ) {
93
95
$ output = array ();
94
96
95
97
switch ($ node ->nodeType ) {
@@ -102,9 +104,11 @@ protected static function &convert($node) {
102
104
break ;
103
105
104
106
case XML_ELEMENT_NODE :
105
-
106
107
// for each child node, call the covert function recursively
107
108
for ($ i =0 , $ m =$ node ->childNodes ->length ; $ i <$ m ; $ i ++) {
109
+ if ($ callback !==null ) {
110
+ $ callback ($ m =$ node ->childNodes ->length , $ i );
111
+ }
108
112
$ child = $ node ->childNodes ->item ($ i );
109
113
$ v = self ::convert ($ child );
110
114
if (isset ($ child ->tagName )) {
0 commit comments