Detect XML self closing tags with PHP XMLReader

一世执手 提交于 2019-12-06 11:02:37

Detection through class property $isEmptyElement does also not work because the tag has attributes.

That's simply wrong. An empty element with attributes is still empty and $isEmptyElement will reflect that. The problem with your code is that you test $isEmptyElement after moving to the attributes. This will change the current node to an attribute node which isn't an empty element. Something like the following should work:

        $isEmpty = $xmlReader->isEmptyElement;
        if ($xmlReader->hasAttributes) {
            while ($xmlReader->moveToNextAttribute()) {
                ...
            }
        }
        if ($isEmpty) {
            $xmlWriter->endElement();
        }

Or, alternatively:

        if ($xmlReader->hasAttributes) {
            while ($xmlReader->moveToNextAttribute()) {
               ...
            }
            $xmlReader->moveToElement();
        }
        if ($xmlReader->isEmptyElement) {
            $xmlWriter->endElement();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!