V
V
Vladimir Semenok2019-01-25 16:55:51
PHP
Vladimir Semenok, 2019-01-25 16:55:51

How to configure XML parsing with XMLReader in PHP for a specific vendor?

There is an online store running on CMS 1C-Bitrix. Product data is received on the site after processing xml files from 7 different suppliers. After changing one of the parameter providers, the data on the balances is no longer displayed in the xml file. Other suppliers did not have this problem. Prices are processed using XMLReader in PHP. As far as I understand, a line is taken from the price list of the supplier, in which (of which) the quantity of goods is described:

<param name="status">Ожидается</param>
<param name="status">В наличии</param>

or Here are the working parameters from the init.php file:
<stock1>1</stock1>
case "param":
          if("status" != $xmlReader->getAttribute('name') && "Status" != $xmlReader->getAttribute('name')) continue;
          $xmlReader->read();
          
          if($xmlReader->nodeType == XMLReader::TEXT) {
            
            $text = $xmlReader->value;
            
            if(strpos($text,"В наличии") !== false){
              $result['available'] = true;
            }
            if(strpos($text,"менее") !== false){
              $result['available'] = true;
            }
          }
          break;
        case "stock":
          $xmlReader->read();
          if($xmlReader->nodeType == XMLReader::TEXT) {
            $stock = (string)$xmlReader->value;
            $stock = preg_replace("/[^0-9]/", '', strip_tags($stock));
            $result['available'] = (intVal($stock) > 0);
          }
          break;

The problematic supplier in the xml file used to have "in stock" and "out of stock" parameters, now they are:
<available>false</available>
<available>true</available>

What needs to be added to the init.php file so that the remaining goods of this supplier are displayed correctly?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Semenok, 2019-01-26
@semenukacola

The problem was solved by adding the following code:

case "available":
                     $xmlReader->read();
  
                    if($xmlReader->nodeType == XMLReader::TEXT) {
            $text = (string)$xmlReader->value;
            if($text == 'true') {
              $result['available'] = true;
            } else {
              $result['available'] = false;
            }     
                    }
                    break;

F
Farwisdomer, 2019-01-25
@Farwisdomer

My take is to standardize the look of xml by injecting a dtd or schema. And describe accessibility as an attribute, for example <param name="status", available="true">. And so you will edit the code indefinitely, one supplier will send <available>false</available>, the second <available>no</available>, and the third <available>да нащальникамана, есть</available>. The DTD will strictly specify the allowed attribute values ​​and the default value. Then the parser will be the same for all customers.

S
Sergey, 2019-05-06
@oldzas

Try this: https://github.com/prewk/xml-string-streamer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question