Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
xml2array.php
1<?php
2
4
10{
15 public static function convert($xmlData, $convertCharset = true)
16 {
17 if($xmlData == '')
18 return array();
19
20 $result = array();
21
22 if($convertCharset && mb_strtolower(SITE_CHARSET) != 'utf-8')
23 $xmlData = \Bitrix\Main\Text\Encoding::convertEncoding($xmlData, SITE_CHARSET, 'UTF-8');
24
25 if(preg_replace('/[[:^print:]]/', '', $xmlData) == "<?xml version='1.0' encoding='UTF-8'?>")
26 return array();
27
28 //$xmlData = preg_replace('/[[:^print:]]/', '', $xmlData);
29 libxml_use_internal_errors(true);
30
31 try
32 {
33 $results = new \SimpleXMLElement($xmlData, LIBXML_NOCDATA);
34 }
35 catch(\Exception $e)
36 {
37 $logger = new Logger;
38 $logger->addRecord(
40 'TRADING_PLATFORM_XML2ARRAY_ERROR',
41 'convert',
42 'Can\'t convert xmlData to SimpleXMLElement. Data: ('.$xmlData.'). Error: '.$e->getMessage()
43 );
44
45 return array();
46 }
47
48 if(!$results)
49 {
50 $logger = new Logger;
51 $logger->addRecord(
53 'TRADING_PLATFORM_XML2ARRAY_ERROR',
54 'convert',
55 'Wrong xmlData format. Data: ('.$xmlData.').'
56 );
57
58 return array();
59 }
60 elseif($jsonString = json_encode($results))
61 {
62 $result = json_decode($jsonString, TRUE);
63 }
64
65 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
66 $result = \Bitrix\Main\Text\Encoding::convertEncoding($result, 'UTF-8', SITE_CHARSET);
67
68 return $result;
69 }
70
75 public static function normalize(array $branch)
76 {
77 reset($branch);
78
79 if(key($branch) !== 0)
80 $branch = array( 0 => $branch);
81
82 return $branch;
83 }
84}
static convert($xmlData, $convertCharset=true)
Definition xml2array.php:15
static normalize(array $branch)
Definition xml2array.php:75