0

My xml:

<products>
<product>
<categoryName>Telewizory</categoryName>
<brandName>Sony</brandName>
<productCode>133</productCode>
<productId>14165691</productId>
<productFullName>Design Thinking dla przedsiębiorców i małych firm.</productFullName>
<productEan>3</productEan>
<productEuroPriceNetto>203.33</productEuroPriceNetto>
<productFrontendPriceNetto>220.22</productFrontendPriceNetto>
<productFastestSupplierQuantity>0</productFastestSupplierQuantity>
<deliveryEstimatedDays>5</deliveryEstimatedDays>
</product>
    <product>
<categoryName>Telewizory</categoryName>
<brandName>Sony</brandName>
<productCode>133</productCode>
<productId>1416569122</productId>
<productFullName>Design not Thinking dla przedsiębiorców i małych firm.</productFullName>
<productEan></productEan>
<productEuroPriceNetto>300.33</productEuroPriceNetto>
<productFrontendPriceNetto>22.85</productFrontendPriceNetto>
<productFastestSupplierQuantity>24</productFastestSupplierQuantity>
<deliveryEstimatedDays>2</deliveryEstimatedDays>
</product>
</products>

I use XmlReader to read and parse large XML files:

$reader = new XMLReader();
        $reader->open($url);

        $count = 0;
        $this_value = '';
        //$products = array();
        while($reader->read()) {
            switch ($reader->nodeType) {
                case XMLReader::ELEMENT:
                    // deal with self-closing tags 
                    if ($reader->isEmptyElement) {
                        ${$reader->name} = '';
                        $products[$count][$reader->name] = '';
                    }
                    break;
                case XMLReader::TEXT:
                case XMLReader::CDATA:
                    // save the value for storage when we get to the end of the element
                    $this_value = $reader->value;
                    break;
                case XMLReader::END_ELEMENT:
                    if ($reader->name == 'product') {
                        $count++;
                // print_r(array($categoryName, $brandName, $productCode, $productId, $productFullName, $productEan, $productEuroPriceNetto, $productFrontendPriceNetto, $productFastestSupplierQuantity, $deliveryEstimatedDays));


                } elseif ($reader->name != 'products') {
                        ${$reader->name} = $this_value;
                        //$products[$count][$reader->name] = $this_value;
                        // set this_value to a blank string to allow for empty tags
                        $this_value = '';
                    }
                    break;
                case XMLReader::WHITESPACE:
                case XMLReader::SIGNIFICANT_WHITESPACE:
                default:

                    // nothing to do
                    break;
            }
        }

        $reader->close();

Facing the problem, when connection with the server interrupting. I need to stop this process in this case. How? Seems here it is not possible validate whole file, but maybe are possible validate each part of this large file by element? And it is possible without XSD schema file? Can anybody put me on the right way or maybe someone already have solution for this?

K. B.
  • 1,388
  • 2
  • 13
  • 25
  • Have you tried this [XMLReader::isValid](http://php.net/manual/en/xmlreader.isvalid.php) make sure to read the instruction on the link. I am not sure if it will work, as there are some cavets to using it. One thought is how large is the file, and is it possible to store it locally. That way you can bypass the whole issue. For example instead of starting with this `$reader->open($url);` you could do `file_put_contents($url, $dest); $reader->open($dest);` Then after it's downloaded you can read it with no issues. I've only done local XML reading with XMLReader, so I have had this issue. – ArtisticPhoenix Mar 13 '19 at 17:43
  • Have a look at https://stackoverflow.com/questions/1835177/how-to-use-xmlreader-in-php, you may be able to read each item at a time and track where you have got up to at a higher level than individual elements. – Nigel Ren Mar 13 '19 at 17:45
  • @ ArtisticPhoenix What will happen, if whole xml file not been downloaded, if connection with the remote server will be interrupted? – K. B. Mar 13 '19 at 20:10

0 Answers0