What are the simple options for converting a yml-file (xml) of Yandex.Market to CSV ? Looking for all the data article in one line.
Example file: https://yandex.ru/support/partnermarket/yml/about-yml.xml#pricelist
What are the simple options for converting a yml-file (xml) of Yandex.Market to CSV ? Looking for all the data article in one line.
Example file: https://yandex.ru/support/partnermarket/yml/about-yml.xml#pricelist
if you are only interested in linear data (without arrays and nested structures), then you can use the programs from the xml2 package, which is most likely included in the repositories of all popular distributions of the gnu / linux operating system.
if you copy and save in the primer file what is shown on your link, then you can extract (linear) data like this, presenting it in csv format:
$ cat primer | sed '1s/windows-1251/utf-8/' | xml2 | \ grep /yml_catalog/shop/offers/offer | \ 2csv /yml_catalog/shop/offers/offer @id name price 158,Π‘ΠΌΠ°ΡΡΡΠΎΠ½ Apple iPhone 6s 128gb Space Gray,55690 159,ΠΠ°ΡΡΠ½ΠΈΠΊΠΈ Koss Sporta Pro,3045.5 One freelancer did, here's a working version on php:
<?php ignore_user_abort(1); error_reporting(0); header("Content-Type: text/html; charset=utf-8"); $cat = array(); $xml = new XMLReader(); $xml->open('./in.xml'); while($xml->read() && $xml->name !== 'category'); while($xml->name === 'category'){ $node = new SimpleXMLElement($xml->readOuterXML()); $id = +$xml->getAttribute("id"); $name = ''. $node; $cat[$id] = $name; $xml->next('category'); } $xml->close(); $xml = new XMLReader(); $xml->open('./in.xml'); $allparams = array(); $idparams = array(); while($xml->read() && $xml->name !== 'offer'); while($xml->name === 'offer'){ $node = new SimpleXMLElement($xml->readOuterXML()); $id = +$xml->getAttribute("id"); foreach ($node->param as $param){ $name = ''. $param['name']; $value = ''. $param; $allparams[] = ''. $param['name']; $idparams[$id][$name] = $value; } $xml->next('offer'); } $xml->close(); $allparams = array_unique($allparams); sort($allparams); $xml = new XMLReader(); $xml->open('./in.xml'); $flag = true; while($xml->read() && $xml->name !== 'offer'); while($xml->name === 'offer'){ $node = new SimpleXMLElement($xml->readOuterXML()); $id = $xml->getAttribute("id"); $available = $xml->getAttribute("available"); $url = $node->url; $price = $node->price; $currencyId = $node->currencyId; $delivery = $node->delivery; $local_delivery_cost = $node->local_delivery_cost; $typePrefix = $node->typePrefix; $vendor = $node->vendor; $vendorCode = $node->vendorCode; $model = $node->model; $description = $node->description; $cpa = $node->cpa; $weight = $node->weight; $pickup = $node->pickup; $c = +$node->categoryId; $category = $cat[$c]; $picture = ''; foreach ($node->picture as $pic){ $picture .= $pic .' '; } if($flag){ $str = '"id";"ΠΠ°Π»ΠΈΡΠΈΠ΅";"url ΡΠΎΠ²Π°ΡΠ°";"Π¦Π΅Π½Π°";"ΠΠ°Π»ΡΡΠ°";"ΠΠΎΡΡΠ°Π²ΠΊΠ°";"Π¦Π΅Π½Π° Π΄ΠΎΡΡΠ°Π²ΠΊΠΈ";"Π’ΠΈΠΏ";"ΠΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡΠ΅Π»Ρ";"ΠΠΎΠ΄ ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡΠ΅Π»Ρ";"ΠΠΎΠ΄Π΅Π»Ρ";"ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅";"ΠΠ°ΠΊΠ°Π· Π½Π° ΠΌΠ°ΡΠΊΠ΅ΡΠ΅";"ΠΠ΅Ρ";"url ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ";"Π‘Π°ΠΌΠΎΠ²ΡΠ²ΠΎΠ·";"ΠΠ°ΡΠ΅Π³ΠΎΡΠΈΡ";'; $j = count($allparams); while($j-- > 0){ $str .= '"'. $allparams[$j] .'";'; } $str .= PHP_EOL; $str = mb_convert_encoding ($str ,"Windows-1251" , "UTF-8" ); $path = fopen("out.csv", "a+"); fwrite($path, $str); fclose($path); $flag = false; } $params = ''; $j = count($allparams); while($j-- > 0){ $params .= '"'. $idparams[$id][$allparams[$j]] .'";'; } $str = '"'. $id .'";'; $str .= '"'. $available .'";'; $str .= '"'. $url .'";'; $str .= '"'. $price .'";'; $str .= '"'. $currencyId .'";'; $str .= '"'. $delivery .'";'; $str .= '"'. $local_delivery_cost .'";'; $str .= '"'. $typePrefix .'";'; $str .= '"'. $vendor .'";'; $str .= '"'. $vendorCode .'";'; $str .= '"'. $model .'";'; $str .= '"'. $description .'";'; $str .= '"'. $cpa .'";'; $str .= '"'. $weight .'";'; $str .= '"'. $picture .'";'; $str .= '"'. $pickup .'";'; $str .= '"'. $category .'";'; $str .= $params; $str .= PHP_EOL; $str = mb_convert_encoding ($str ,"Windows-1251" , "UTF-8" ); $path = fopen("out.csv", "a+"); fwrite($path, $str); fclose($path); $xml->next('offer'); } $xml->close(); gc_enable(); echo 'ok'; ?> Source: https://ru.stackoverflow.com/questions/523418/
All Articles