Вывод данных в xml - amel-post/bitrix.help GitHub Wiki

<?php

define("NO_KEEP_STATISTIC", "Y");
define("NO_AGENT_STATISTIC","Y");
define("NOT_CHECK_PERMISSIONS", true);

set_time_limit(0);
ignore_user_abort(true);

require ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

header("Content-Type: text/xml; charset=utf-8");

$shipmentData = [
    'ResultCode' => '0',
    'Location' => [
        'LocationName' => 'г. Москва',
        'PostCode' => '105187',
    ],
    'DeliveryGroups' => [
        'DeliveryGroup' => [
            [
                'GroupName' => 'Курьерская доставка',
                'DeliveryVariants' => [
                    'DeliveryVariant' => [
                        [
                            'DeliveryVariantName' => 'Обычная',
                            'ExternalDeliveryVariantId' => '123',
                            'Description' => 'Скрок доставки 3 рабочих дня',
                            'ItemsCost' => '2400',
                            'DeliveryCost' => '100',
                            'TotalCost' => '2500',
                        ],
                        [
                            'DeliveryVariantName' => 'Медленная',
                            'ExternalDeliveryVariantId' => '124',
                            'Description' => 'Скрок доставки 4 рабочих дня',
                            'ItemsCost' => '2400',
                            'DeliveryCost' => '50',
                            'TotalCost' => '2450',
                        ],
                    ]
                ],
            ],
            [
                'GroupName' => 'Пункты самовывоза',
                'DeliveryVariants' => [
                    'DeliveryVariant' => [
                        [
                            'DeliveryVariantName' => 'Экспесс самовывоз',
                            'ExternalDeliveryVariantId' => '125',
                            'PickupPoints' => [
                                'PickupPoint' => [
                                    [
                                        'Name' => 'г. Москва, ул. Победы 5. MYCOMPANY',
                                        'ExternalPickupPointId' => '234234234',
                                        'DeliveryVariantName' => 'Экспесс самовывоз',
                                        'ExternalDeliveryVariantId' => '127',
                                        'Address' => 'г. Москва, ул. Победы 5',
                                        'Phones' => [
                                            'Phone' => [
                                                '7 916 321 54 76',
                                                '7 495 876 55 55 доб. 5'
                                            ]
                                        ],
                                        'OperatingHours' => [
                                            'OperatingHour' => [
                                                'пн-вс с 9:00 по 23:00'
                                            ]
                                        ],
                                        'Description' => 'Срок доставки: 3 дня, вы будете оповещены по SMS. Срок хранения: неделя',
                                        'ItemsCost' => '2400',
                                        'DeliveryCost' => '52',
                                        'TotalCost' => '2452',
                                    ],
                                    [
                                        'Name' => 'г. Москва, ул. Победы 55. MYCOMPANY',
                                        'ExternalPickupPointId' => '234234235',
                                        'DeliveryVariantName' => 'Экспесс самовывоз',
                                        'ExternalDeliveryVariantId' => '129',
                                        'Address' => 'г. Москва, ул. Победы 55',
                                        'Phones' => [
                                            'Phone' => [
                                                '7 916 321 54 76',
                                                '7 495 876 55 55 доб. 5'
                                            ]
                                        ],
                                        'OperatingHours' => [
                                            'OperatingHour' => [
                                                'пн-вс с 9:00 по 23:00'
                                            ]
                                        ],
                                        'Description' => 'Срок доставки: 3 дня, вы будете оповещены по SMS. Срок хранения: неделя',
                                        'ItemsCost' => '2400',
                                        'DeliveryCost' => '57',
                                        'TotalCost' => '2457',
                                    ]
                                ]
                            ],
                            'ItemsCost' => '2400',
                            'DeliveryCost' => '55',
                            'TotalCost' => '2455',
                        ]
                    ]
                ],
            ]
        ]
    ],
];

function array_to_xml( $data, &$xml_data, $parent = null ) {
    foreach( $data as $key => $value ) {
        if( is_array($value) ) {
            if( is_numeric($key) ){
                if ($key == 0) {
                    array_to_xml($value, $xml_data);
                } else {
                    $subnode = $parent->addChild($xml_data->getName());
                    array_to_xml($value, $subnode);
                }
            } else {
                if (isset($value[0]) && !is_array($value[0])) {
                    foreach ($value as $valueVariant) {
                        $xml_data->addChild("$key", htmlspecialchars("$valueVariant"));
                    }
                } else {
                    $subnode = $xml_data->addChild($key);
                    array_to_xml($value, $subnode, $xml_data);
                }
            }
        } else {
            if( is_numeric($key) ){
                $parent->addChild($xml_data->getName(), htmlspecialchars("$value"));
            } else {
                $xml_data->addChild("$key",htmlspecialchars("$value"));
            }
        }
    }
}

$xml_data = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><GetDeliveryVariantsResult xmlns="http://tempuri.org/XMLSchema.xsd" />');
array_to_xml($shipmentData,$xml_data);
print $xml_data->asXML();