Использую расширение от Hobnob\XmlStreamReader\Parser для yii2
Код: Выделить всё
$parser = new \Hobnob\XmlStreamReader\Parser();
$parser->registerCallback(
'/source/products/product',
function( \Hobnob\XmlStreamReader\Parser $parser, \SimpleXMLElement $node ) {
$this->parseListing($node);
}
);
Код: Выделить всё
protected function parseListing($product)
{
if (empty($product))
return;
$data = $this->xml2array($product);
/*тут уже сохраняю к себе в базу данные из $data*/
}
protected function xml2array($xml)
{
$arr = array();
foreach ($xml->children() as $r)
{
/** @var \SimpleXMLElement $r */
if (count($r->children()) == 0)
$arr[$r->getName()] = strval($r);
else
$arr[$r->getName()][] = $this->xml2array($r);
}
return $arr;
}