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

  • in the same place nonlinear data (in an example under the link). there are both arrays and nested structures. How do you imagine this vinaigrette in the form of csv? // part of the information (the one that is β€œlinear”) can be converted using the xml2 package (must be present in popular distributions). - aleksandr barakin
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

2 answers 2

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'; ?>