Задача сформировать файл XML из JSON данных. Вручную получается собрать такой файл, а вот как из массива не могу понять.
Формат XML файла нужен такой:
<?xml version="1.0" encoding="utf-8"?>
<kaspi_catalog date="string"
xmlns="kaspiShopping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="kaspiShopping http://kaspi.kz/kaspishopping.xsd">
<company>CompanyName</company>
<merchantid>CompanyID</merchantid>
<offers>
<offer sku="232130213">
<model>iphone 5s white 32gb</model>
<brand>Apple</brand>
<availabilities>
<availability available="yes" storeId="myFavoritePickupPoint1"/>
<availability available="yes" storeId="myFavoritePickupPoint2"/>
</availabilities>
<price>6418</price>
</offer>
<offer sku="232130223">
<model>iphone 6s white 32gb</model>
<brand>Apple</brand>
<availabilities>
<availability available="yes" storeId="myFavoritePickupPoint1"/>
<availability available="yes" storeId="myFavoritePickupPoint2"/>
</availabilities>
<cityprices>
<cityprice cityId="750000000">193000</cityprice>
<cityprice cityId="710000000">195000</cityprice>
</cityprices>
</offer>
</offers>
</kaspi_catalog>
Мой код ручного формирования :
<?php
$simplexml= new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?> <kaspi_catalog/>');
$simplexml->addAttribute('xmlns:date', 'string');
$simplexml->addAttribute('xmlns:xmlns', 'kaspiShopping');
$simplexml->addAttribute('xmlns:xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$simplexml->addAttribute('xmlns:xsi:schemaLocation', 'kaspiShopping http://kaspi.kz/kaspishopping.xsd');
$simplexml->addChild("company", "CompanyName");
$simplexml->addChild("merchantid", "CompanyID");
$offers = $simplexml->addChild('offers');
$offer = $offers->addChild('offer');
$offer->addAttribute('offer:sku', '232130213');
$offer->addChild("model", "iphone 5s white 32gb");
$offer->addChild("brand", "Apple");
$availabilities = $offer->addChild("availabilities");
$availabilities->addChild("availabilities");
file_put_contents('price_list.xml', $simplexml->asXML());
?>
Исходный JSON выглядит так:
"balanceArray": [
{
"id": "7",
"productId": "36",
"productCode1c": "",
"code": "759fd581d807dced6a4995c33a395788",
"amount": "1.000",
"amountavaillable": 1,
"currency": "KZT",
"currencyrate": "421.000",
"price": "0.0000",
"productname": "Janome Grape 2016 белый-зеленый",
"serial": "19123879",
"storageName": "Основной ALA"
},