Search code examples
phpxmlxmlreader

Why is this loop running indefinitely?


I have code like this, it reads XML document and copies it to a differently formatted XML.

    $new = '<prestashop>';

    while ($xml_reader->read() and $xml_reader->name !== 'product');

    while ($xml_reader->name === 'product')         // dla kazdego produktu
    {
        $node = new SimpleXMLElement($xml_reader->readOuterXML());
        $new .= '<product>';

        foreach ($this->columns as $out => $in)     // dla kazdej kolumny xml
        {
            if ($node->$xml !== '')                 // jesli ma wartosc
            {
                $new .= "<{$out}>{$node->$in}</{$out}>";
            }
            else
            {
                $new .= "<{$out}/>";
            }
        }
        $new .= '</product>';
    }
    $new .= '</prestashop>';

The XML has this stucture: <product>...</product><product>...</product>. I checked whatever I could and the error is probably in the while.

@edit: I use PHP's XML Reader to get a node one by one and then SimpleXML to deal with the node itself.


Solution

  • It was dumb. I forgot $xml_reader->next('product'); so while loop was always going around the same node all over again.