Search code examples
phpxmlreader

XmlReader Fail To Read Empty Nodes These Like Chrome Reader


i have a problem with reading empty elements formatted like <eanCode/>. After this element, XmlReader class is failing to read next element.

<EanCode/><Stock>15</Stock>

if ($reader->nodeType == XMLReader::ELEMENT) {
            switch ($reader->name) {

           case 'Stock':
                    $reader->read();
                    $stock = $reader->value;
            ECHO 'stokkk adet = '. $stock;
                    break;...

So, I decided to replace <eanCode/> with <eanCode></eanCode>. But it's difficult to write an algorithm. Can anyone supply a corresponding preg replace method, please?


Solution

  • There's an issue in XMLReader.

    If you have an empty element like this: <test></test>

    You would get An ELEMENT and an END_ELEMENT.

    However, if you use a self closing tag: <test />

    You only get an ELEMENT, and NO END_ELEMENT.

    An easy solution is to check if $xmlReader->isEmptyElement on each ELEMENT NODE. This will allow you to know if it's a self closing tag, you can then skip it.