Answer the question
In order to leave comments, you need to log in
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>
<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;
<available>false</available>
<available>true</available>
Answer the question
In order to leave comments, you need to log in
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;
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question