Xml::simpleXMLStringToArray()   C
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 13
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace fangface\tools;
4
5
class Xml
6
{
7
8
    private $array;
9
    private $rootElement;
10
    private $encodeUTF8;
11
    private $decodeUTF8;
12
13
14
    public function __construct($rootElement = '')
15
    {
16
        $this->rootElement = $rootElement;
17
        $this->setDefaults();
18
        $this->reset();
19
    }
20
21
22
    public function setDefaults()
23
    {
24
        $this->encodeUTF8 = true;
25
        $this->decodeUTF8 = false;
26
    }
27
28
29
    public function getRootElement()
30
    {
31
        return $this->rootElement;
32
    }
33
34
35
    public function setEncodeUTF8($value)
36
    {
37
        $this->encodeUTF8 = $value;
38
    }
39
40
41
    public function setDecodeUTF8($value)
42
    {
43
        $this->decodeUTF8 = $value;
44
    }
45
46
47
    public function reset()
48
    {
49
        $this->resetArray();
50
    }
51
52
53
    public function resetArray()
54
    {
55
        $this->array = array();
56
    }
57
58
59
    public function setArray($v, $autoFormat = true)
60
    {
61
        $this->array = array();
62
        if ($v && is_array($v)) {
63
            foreach ($v as $_k => $_v) {
64
                if ($this->rootElement == '') {
65
                    $this->rootElement = $_k;
66
                }
67
            }
68
            if ($autoFormat) {
69
                $this->array[$this->rootElement] = array(
70
                    '@data' => $this->convertToLocalArray($_v)
0 ignored issues
show
Bug introduced by
The variable $_v seems to be defined by a foreach iteration on line 63. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
71
                );
72
            } else {
73
                $this->array[$this->rootElement] = $_v;
74
            }
75
        }
76
    }
77
78
79
    public function set($k, $v, $attribs = false, $autoFormat = true)
80
    {
81
        $allElements = explode(".", $k);
82
        $evalString = '';
83
        $evalStringBase = '';
84
        foreach ($allElements as $e) {
85
            if ($evalString == '' && $this->rootElement == '') {
86
                $this->rootElement = $e;
87
            } elseif ($evalString == '' && $this->rootElement != '' && $this->rootElement != $e) {
88
                $e = $this->rootElement;
89
            }
90
            $evalStringBase = $evalString . '[\'' . $e . '\']';
91
            $evalString .= '[\'' . $e . '\'][\'@data\']';
92
            eval('$isSet = @isset($this->array' . $evalString . ');');
93
            if (!$isSet) {
94
                eval('$this->array' . $evalString . ' = \'\';');
95
            }
96
        }
97
        if ($autoFormat && is_array($v)) {
98
            $v = $this->convertToLocalArray($v);
99
        }
100
        eval('$this->array' . $evalString . ' = $v;');
101
        if ($attribs && $evalStringBase != '') {
102
            eval('$this->array' . $evalStringBase . '[\'@attribs\'] = $attribs;');
103
        }
104
    }
105
106
107
    public function setAttribute($k, $attrib, $v)
108
    {
109
        if ($k != '' && $attrib != '') {
110
            $allElements = explode(".", $k);
111
            $evalString = '';
112
            $evalStringBase = '';
113
            foreach ($allElements as $e) {
114
                if ($evalString == '' && $this->rootElement == '') {
115
                    $this->rootElement = $e;
116
                } elseif ($evalString == '' && $this->rootElement != '' && $this->rootElement != $e) {
117
                    $e = $this->rootElement;
118
                }
119
                $evalStringBase = $evalString . '[\'' . $e . '\']';
120
                $evalString .= '[\'' . $e . '\'][\'@data\']';
121
                eval('$isSet = @isset($this->array' . $evalString . ');');
122
                if (!$isSet) {
123
                    eval('$this->array' . $evalString . ' = \'\';');
124
                }
125
            }
126
            if ($evalStringBase != '') {
127
                eval('$this->array' . $evalStringBase . '[\'@attribs\'][\'' . $attrib . '\'] = $v;');
128
            }
129
        }
130
    }
131
132
133
    public function setAttributes($k, $attribs)
134
    {
135
        if ($k != '' && $attribs && is_array($attribs)) {
136
            $allElements = explode(".", $k);
137
            $evalString = '';
138
            $evalStringBase = '';
139
            foreach ($allElements as $e) {
140
                if ($evalString == '' && $this->rootElement == '') {
141
                    $this->rootElement = $e;
142
                } elseif ($evalString == '' && $this->rootElement != '' && $this->rootElement != $e) {
143
                    $e = $this->rootElement;
144
                }
145
                $evalStringBase = $evalString . '[\'' . $e . '\']';
146
                $evalString .= '[\'' . $e . '\'][\'@data\']';
147
                eval('$isSet = @isset($this->array' . $evalString . ');');
148
                if (!$isSet) {
149
                    eval('$this->array' . $evalString . ' = \'\';');
150
                }
151
            }
152
            if ($evalStringBase != '') {
153
                eval('$this->array' . $evalStringBase . '[\'@attribs\'] = $attribs;');
154
            }
155
        }
156
    }
157
158
159
    public function addToArray($k, $v, $attribs = false, $autoFormat = true)
160
    {
161
        $allElements = explode(".", $k);
162
        $evalString = '';
163
        $evalStringBase = '';
164
        foreach ($allElements as $e) {
165
            if ($evalString == '' && $this->rootElement == '') {
166
                $this->rootElement = $e;
167
            } elseif ($evalString == '' && $this->rootElement != '' && $this->rootElement != $e) {
168
                $e = $this->rootElement;
169
            }
170
            $evalStringBase = $evalString . '[\'' . $e . '\']';
171
            $evalString .= '[\'' . $e . '\'][\'@data\']';
172
            eval('$isSet = @isset($this->array' . $evalString . ');');
173
            if (!$isSet) {
174
                eval('$this->array' . $evalString . ' = \'\';');
175
            }
176
        }
177
        eval('$isArray = is_array($this->array' . $evalString . ');');
178
        if (!$isArray) {
179
            eval('$this->array' . $evalString . ' = array();');
180
        }
181
        if ($autoFormat && is_array($v)) {
182
            $v = $this->convertToLocalArray($v);
183
        }
184
        eval('$this->array' . $evalString . '[] = array(\'@data\' => $v,\'@attribs\' => $attribs);');
185
    }
186
187
188
    public function convertToLocalArray($array)
189
    {
190
        $retVal = array();
191
        if ($array && is_array($array)) {
192
            foreach ($array as $k => $v) {
193
                $isMulti = false;
194
                if ($isArray = is_array($v)) {
195
                    $arrayKeys = array_keys($v);
196
                    if ($arrayKeys[0] == '0') {
197
                        $isMulti = true;
198
                    }
199
                }
200
                if ($isArray) {
201
                    if ($isMulti) {
202
                        foreach ($array as $k2 => $v2) {
203
                            $retVal[$k]['@data'][$k2] = $this->convertToLocalArray($v2);
204
                        }
205
                    }
206
                    $retVal[$k]['@data'] = $this->convertToLocalArray($v);
207
                } else {
208
                    $retVal[$k]['@data'] = $v;
209
                }
210
            }
211
        }
212
        return $retVal;
213
    }
214
215
216
    public function getDocument($forDisplay = false, $incPre = false)
217
    {
218
        if ($forDisplay) {
219
            $retVal = $this->writeXML($this->array, '', 0, true, '  ');
220
            if ($incPre) {
221
                return '<pre>' . str_replace(array(
222
                    '<',
223
                    '>'
224
                ), array(
225
                    '&lt;',
226
                    '&gt;'
227
                ), $retVal) . '</pre>';
228
            } else {
229
                return str_replace(array(
230
                    '<',
231
                    '>'
232
                ), array(
233
                    '&lt;',
234
                    '&gt;'
235
                ), $retVal);
236
            }
237
        } else {
238
            $retVal = $this->writeXML($this->array);
239
            return $retVal;
240
        }
241
    }
242
243
244
    public function writeXML($array, $branchIn = '', $level = 0, $useLongTag = true, $indentString = "\t")
245
    {
246
        $xml = '';
247
        $indent = ($level > 0 ? $this->repli($indentString, $level) : '');
248
        foreach ($array as $k => $v) {
249
            $branch = ($branchIn != '' ? $branchIn . '.' : '') . $k;
250
            $isMulti = false;
251
            if ($isArray = (isset($v['@data']) && is_array($v['@data']))) {
252
                $arrayKeys = array_keys($v['@data']);
253
                if ($arrayKeys[0] == '0') {
254
                    $isMulti = true;
255
                }
256
            }
257
            if ($isMulti) {
258
                $loopVar = $v['@data'];
259
                foreach ($loopVar as $k2 => $v2) {
260
                    if (is_array($v2['@data']) || is_object($v2['@data'])) {
261
                        $xml .= $indent . '<' . $k . $this->setXMLAttr($v2) . '>' . "\n";
262
                        $xml .= $this->writeXML($v2['@data'], $branch, ($level + 1), $useLongTag, $indentString);
263
                        $xml .= $indent . '</' . $k . '>' . "\n";
264
                    } else {
265
                        if ((isset($v2['@data']) && $v2['@data']) || (isset($v2['@attribs']) && $v2['@attribs']) || $useLongTag) {
266
                            $xml .= $indent . '<' . $k . $this->setXMLAttr($v2) . '>';
267
                            if (isset($v2['@data'])) {
268
                                $xml .= $this->encode($v2['@data']);
269
                            }
270
                            $xml .= '</' . $k . '>' . "\n";
271
                        } else {
272
                            $xml .= $indent . '<' . $k . '/>' . "\n";
273
                        }
274
                    }
275
                }
276
            } elseif ($isArray) {
277
                $xml .= $indent . '<' . $k . $this->setXMLAttr($v) . '>' . "\n";
278
                $xml .= $this->writeXML($v['@data'], $branch, ($level + 1), $useLongTag, $indentString);
279
                $xml .= $indent . '</' . $k . '>' . "\n";
280
            } else {
281
                if ((isset($v['@data']) && $v['@data']) || (isset($v['@attribs']) && $v['@attribs']) || $useLongTag) {
282
                    $xml .= $indent . '<' . $k . $this->setXMLAttr($v) . '>';
283
                    if (isset($v['@data'])) {
284
                        $xml .= $this->encode($v['@data']);
285
                    }
286
                    $xml .= '</' . $k . '>' . "\n";
287
                } else {
288
                    $xml .= $indent . '<' . $k . '/>' . "\n";
289
                }
290
            }
291
        }
292
        return $xml;
293
    }
294
295
296
    public function setXMLAttr($element)
297
    {
298
        $retVal = '';
299
        if (isset($element['@attribs']) && $element['@attribs'] && is_array($element['@attribs'])) {
300
            $loopVar = $element['@attribs'];
301
            foreach ($loopVar as $k => $v) {
302
                $retVal .= ' ' . $k . '="' . $this->encode($v, true) . '"';
303
            }
304
        }
305
        return $retVal;
306
    }
307
308
309
    public function encode($value, $isAttr = false)
310
    {
311
        if (!$this->encodeUTF8 || $this->is_utf8($value)) {
312
            $encoded_data = $value;
313
        } else {
314
            $encoded_data = $this->utf8_encode($value);
315
        }
316
        $escaped_data = htmlspecialchars($encoded_data);
317
        if ($escaped_data != $encoded_data) {
318
            $escaped_data = '<![CDATA[' . $escaped_data . ']]>';
319
        } else {
320
            $escaped_data = $encoded_data;
321
        }
322
        return $escaped_data;
323
    }
324
325
326
    public function decode($value)
327
    {
328
        if ($this->decodeUTF8 && $this->is_utf8($value)) {
329
            $retVal = $this->utf8_decode($value);
330
        } else {
331
            $retVal = $value;
332
        }
333
        return $retVal;
334
    }
335
336
337
    public function is_utf8($string)
338
    { // v1.01
339
        $_is_utf8_split = 4000;
340
        if (strlen($string) > $_is_utf8_split) {
341
            // Based on: http://mobile-website.mobi/php-utf8-vs-iso-8859-1-59
342
            for ($i = 0, $s = $_is_utf8_split, $j = ceil(strlen($string) / $_is_utf8_split); $i < $j; $i++, $s += $_is_utf8_split) {
343
                if ($this->is_utf8(substr($string, $s, $_is_utf8_split)))
344
                    return true;
345
            }
346
            return false;
347
        } else {
348
            return preg_match('%^(?:
349
				[\x09\x0A\x0D\x20-\x7E]              # ASCII
350
				| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
351
				|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
352
				| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
353
				|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
354
				|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
355
				| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
356
				|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
357
			)*$%xs', $string);
358
        }
359
    }
360
361
362
    public function getAttribute($k, $attrib = '')
363
    {
364
        return $this->get($k, $attrib);
365
    }
366
367
368
    public function get($k, $attrib = '', $raw = false, $defaultReturn = false, $isSetCheck = false)
369
    {
370
        $retVal = $defaultReturn;
371
        $wasSet = false;
372
        if ($k != '' && $this->array) {
373
            $allElements = explode(".", $k);
374
            $evalString = '';
375
            $evalStringBase = '';
376
            foreach ($allElements as $e) {
377
                $evalStringBase = $evalString . '[\'' . $e . '\']';
378
                $evalString .= '[\'' . $e . '\'][\'@data\']';
379
            }
380
            if ($attrib != '') {
381
                eval('$isSet = isset($this->array' . $evalStringBase . '[\'@attribs\']);');
382
                if ($isSet) {
383
                    if ($attrib == 'ALL-ATTRIBS') {
384
                        $wasSet = true;
385
                        eval('$retVal = $this->array' . $evalStringBase . '[\'@attribs\'];');
386
                    } else {
387
                        eval('$isSet = isset($this->array' . $evalStringBase . '[\'@attribs\'][\'' . $attrib . '\']);');
388
                        if ($isSet) {
389
                            $wasSet = true;
390
                            eval('$retVal = $this->array' . $evalStringBase . '[\'@attribs\'][\'' . $attrib . '\'];');
391
                        }
392
                    }
393
                }
394
            } else {
395
                eval('$isSet = isset($this->array' . $evalStringBase . '[\'@data\']);');
396
                if ($isSet) {
397
                    $wasSet = true;
398
                    eval('$retVal = $this->array' . $evalStringBase . '[\'@data\'];');
399
                    if (!$raw) {
400
                        if (is_array($retVal)) {
401
                            $retVal = $this->dataOnlyFromArray($retVal);
402
                        }
403
                    }
404
                } else {
405
                    if ($isSetCheck) {
406
                        eval('$isSet = isset($this->array' . $evalStringBase . ');');
407
                        if ($isSet) {
408
                            $wasSet = true;
409
                        }
410
                    }
411
                }
412
            }
413
        }
414
415
        if ($isSetCheck) {
416
            $retVal = $wasSet;
417
        }
418
419
        return $retVal;
420
    }
421
422
423
    public function get2($k, $defaultReturn = '', $attrib = '', $raw = false)
424
    {
425
        return $this->get($k, $attrib, $raw, $defaultReturn);
426
    }
427
428
429
    public function getIsSet($k, $attrib = '')
430
    {
431
        return $this->get($k, $attrib, false, false, true);
432
    }
433
434
435
    public function getMulti($k)
436
    {
437
        $retVal = $this->get($k);
438
        if ($retVal && is_array($retVal)) {
439
            $arrayKeys = array_keys($retVal);
440
            if ($arrayKeys[0] == '0') {
441
                // leave as is
442
            } else {
443
                $retVal = array(
444
                    $retVal
445
                );
446
            }
447
        }
448
        return $retVal;
449
    }
450
451
452
    public function getRaw($k, $attrib = '', $defaultReturn = false)
453
    {
454
        return $this->get($k, $attrib, true, $defaultReturn);
455
    }
456
457
458
    public function dataOnlyFromArray($thisArray)
459
    {
460
        $retVal = array();
461
        if ($thisArray && is_array($thisArray)) {
462
            foreach ($thisArray as $thisArrayKey => $thisArrayValue) {
463
                $thisValue = (isset($thisArrayValue['@data']) ? $thisArrayValue['@data'] : $thisArrayValue);
464
                if (is_array($thisValue) && $thisValue) {
465
                    $retVal[$thisArrayKey] = $this->dataOnlyFromArray($thisValue);
466
                } else {
467
                    $retVal[$thisArrayKey] = ($thisValue == array() ? '' : $thisValue);
468
                }
469
            }
470
        } else {
471
            $retVal = $thisArray;
472
        }
473
        return $retVal;
474
    }
475
476
477
    public function getArray($dataOnly = false)
478
    {
479
        if ($dataOnly && $this->array) {
480
            return $this->dataOnlyFromArray($this->array);
481
        } else {
482
            return $this->array;
483
        }
484
    }
485
486
487
    /**
488
     * @param string $strInput
489
     * @param integer $intCount
490
     */
491
    public function repli($strInput, $intCount)
492
    {
493
        $strResult = '';
494
        for ($i = 0; $i < $intCount; $i++) {
495
            $strResult = $strResult . $strInput;
496
        }
497
        return $strResult;
498
    }
499
500
501
    /**
502
     * Converts a simpleXML element into an array.
503
     * Preserves attributes and everything.
504
     * You can choose to get your elements either flattened, or stored in a custom index that
505
     * you define.
506
     * For example, for a given element
507
     * <field name="someName" type="someType"/>
508
     * if you choose to flatten attributes, you would get:
509
     * $array['field']['name'] = 'someName';
510
     * $array['field']['type'] = 'someType';
511
     * If you choose not to flatten, you get:
512
     * $array['field']['@attributes']['name'] = 'someName';
513
     * _____________________________________
514
     * Repeating fields are stored in indexed arrays. so for a markup such as:
515
     * <parent>
516
     * <child>a</child>
517
     * <child>b</child>
518
     * <child>c</child>
519
     * </parent>
520
     * you array would be:
521
     * $array['parent']['child'][0] = 'a';
522
     * $array['parent']['child'][1] = 'b';
523
     * ...And so on.
524
     * _____________________________________
525
     *
526
     * @param \SimpleXMLElement $xml
527
     *        the XML to convert
528
     * @param boolean $flattenValues
529
     *        wether to flatten values
530
     *        or to set them under a particular index.
531
     *        defaults to false;
532
     * @param boolean $flattenAttributes
533
     *        wether to flatten attributes
534
     *        or to set them under a particular index.
535
     *        Defaults to false;
536
     * @param boolean $flattenChildren
537
     *        wether to flatten children
538
     *        or to set them under a particular index.
539
     *        Defaults to false;
540
     * @param string $valueKey
541
     *        for values, in case $flattenValues was set to
542
     *        false. Defaults to "@data"
543
     * @param string $attributesKey
544
     *        for attributes, in case $flattenAttributes was set to
545
     *        false. Defaults to "@attribs"
546
     * @param string $childrenKey
547
     *        for children, in case $flattenChildren was set to
548
     *        false. Defaults to "@data"
549
     * @return array the resulting array.
550
     */
551
    public function simpleXMLToArray($xml, $flattenValues = false, $flattenAttributes = false, $flattenChildren = false, $valueKey = '@data', $attributesKey = '@attribs', $childrenKey = '@data')
552
    {
553
        $return = array();
554
        if (!($xml instanceof \SimpleXMLElement)) {
555
            return $return;
556
        }
557
        $name = $xml->getName();
558
        $_value = ((string) $xml);
559
        if (strlen($_value) == 0) {
560
            $_value = null;
561
        }
562
        ;
563
564
        if ($_value !== null) {
565
            if (!$flattenValues) {
566
                $return[$valueKey] = $_value;
567
            } else {
568
                $return = $_value;
569
            }
570
        }
571
572
        $children = array();
573
        $first = true;
574
        foreach ($xml->children() as $elementName => $child) {
575
            $value = $this->simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
576
            if (isset($children[$elementName])) {
577
                if ($first) {
578
                    $temp = $children[$elementName];
579
                    unset($children[$elementName]);
580
                    $children[$elementName]['@data'][] = $temp;
581
                    $first = false;
582
                }
583
                $children[$elementName]['@data'][] = $value;
584
            } else {
585
                $children[$elementName] = $value;
586
            }
587
        }
588
        if (count($children) > 0) {
589
            if (!$flattenChildren) {
590
                $return[$childrenKey] = $children;
591
            } else {
592
                $return = array_merge($return, $children);
593
            }
594
        }
595
596
        $attributes = array();
597
        foreach ($xml->attributes() as $name => $value) {
598
            $attributes[$name] = trim($value);
599
        }
600
        if (count($attributes) > 0) {
601
            if (!$flattenAttributes) {
602
                $return[$attributesKey] = $attributes;
603
            } else {
604
                $return = array_merge($return, $attributes);
605
            }
606
        }
607
608
        return $return;
609
    }
610
611
612
    public function simpleXMLStringToArray($XMLData, $rootElement = '', $flattenValues = false, $flattenAttributes = false, $flattenChildren = false, $valueKey = '@data', $attributesKey = '@attribs', $childrenKey = '@data')
613
    {
614
        $XMLData = trim($XMLData);
615
        if ($XMLData == '') {
616
            return false;
617
        }
618
619
        $rootElement = ($rootElement != '' ? $rootElement : $this->rootElement);
620
621
        libxml_clear_errors();
622
        $_oldValue = libxml_use_internal_errors(true);
623
624
        $xml = simplexml_load_string($XMLData);
625
        $retVal = $this->simpleXMLToArray($xml, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
626
627
        if ($errors = libxml_get_errors()) {
628
            $retVal = false;
629
            foreach ($errors as $error) {
630
                trigger_error('simpleXMLFileToArray error ' . $error->code . ' : ' . trim(str_replace("\n", ' ', $error->message)) . ' : ' . $error->file . ' : ' . $error->line);
631
            }
632
        }
633
        libxml_use_internal_errors($_oldValue);
634
        libxml_clear_errors();
635
636
        if ($retVal) {
637
            return ($rootElement != '' ? array($rootElement => $retVal) : $retVal);
638
        } else {
639
            return false;
640
        }
641
    }
642
643
644
    public function simpleXMLFileToArray($XMLFile, $rootElement = '', $flattenValues = false, $flattenAttributes = false, $flattenChildren = false, $valueKey = '@data', $attributesKey = '@attribs', $childrenKey = '@data')
645
    {
646
        if ($XMLFile == '') {
647
            return false;
648
        }
649
650
        $rootElement = ($rootElement != '' ? $rootElement : $this->rootElement);
651
652
        libxml_clear_errors();
653
        $_oldValue = libxml_use_internal_errors(false);
654
655
        $xml = simplexml_load_file($XMLFile);
656
        $retVal = $this->simpleXMLToArray($xml, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
657
658
        if ($errors = libxml_get_errors()) {
659
            $retVal = false;
660
            foreach ($errors as $error) {
661
                trigger_error('simpleXMLFileToArray error ' . $error->code . ' : ' . $error->message . ' : ' . $error->file . ' : ' . $error->line);
662
            }
663
        }
664
        libxml_use_internal_errors($_oldValue);
665
        libxml_clear_errors();
666
667
        if ($retVal) {
668
            return ($rootElement != '' ? array(
669
                $rootElement => $retVal
670
            ) : $retVal);
671
        } else {
672
            return false;
673
        }
674
    }
675
676
677
    public function readDocumentFromFile($XMLFile, $rootElement = '')
678
    {
679
        $rootElement = ($rootElement != '' ? $rootElement : $this->rootElement);
680
        $this->resetArray();
681
        $array = $this->simpleXMLFileToArray($XMLFile, $rootElement);
682
        if ($array) {
683
            $this->setArray($array, false);
684
        }
685
        return ($array ? true : false);
686
    }
687
688
689
    public function readDocumentFromString($XMLData, $rootElement = '')
690
    {
691
        $rootElement = ($rootElement != '' ? $rootElement : $this->rootElement);
692
        $this->resetArray();
693
        $array = $this->simpleXMLStringToArray($XMLData, $rootElement);
694
        if ($array) {
695
            $this->setArray($array, false);
696
        }
697
        return ($array ? true : false);
698
    }
699
700
}
701