简体   繁体   English

使用PHP XMLReader检测XML自动关闭标签

[英]Detect XML self closing tags with PHP XMLReader

I'd like to parse one XML document using XMLReader. 我想使用XMLReader解析一个XML文档。 I have a case switch with all the Constants. 我有一个带有所有常量的大小写开关。 However, if a Tag is self-closing XMLReader only fires ELEMENT, not ELEMENT and than END_ELEMENT like expected. 但是,如果Tag是自动关闭的XMLReader,则仅触发ELEMENT,而不触发ELEMENT,并且比预期的END_ELEMENT触发。

Detection through class property $isEmptyElement does also not work because the tag has attributes. 通过类属性$ isEmptyElement进行的检测也不起作用,因为标记具有属性。

Therefore my question: How to detect a self-closing XML tag with XMLReader in PHP? 因此,我的问题是: 如何在PHP中使用XMLReader检测自动关闭的XML标签?

Related but no solution: XmlReader - Self-closing element does not fire a EndElement event? 相关但无解决方案: XmlReader-自关闭元素不会触发EndElement事件?

Example Node: 示例节点:

<mynode name="somenamestring" code="intstring" option="intstring3"/>

My Code: 我的代码:

$xmlReader->open($url,NULL);
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString('    ');
while ($xmlReader->read()) {
    switch ($xmlReader->nodeType) {
        case 1: #element
            $xmlWriter->startElement($xmlReader->name);
            if ($xmlReader->hasAttributes) {
                while ($xmlReader->moveToNextAttribute()) {
                    $xmlWriter->writeAttribute($xmlReader->name,$xmlReader->value);
                }
            }
            if ($xmlReader->isEmptyElement) {
                $xmlWriter->endElement();
            }
            break;

        case 3: #text
            $xmlWriter->text($xmlReader->value);
            break;

        case 4: #cdata
            $xmlWriter->writeCData($xmlReader->value);
            break;

        case 14: #whitespace
            break;

        case 15: #end element
            $xmlWriter->endElement();
            break;

        default:
            print('[WARN] NodeType not in case-switch: '.(string)$xmlReader->nodeType."\n");
            break;
    }
}

Detection through class property $isEmptyElement does also not work because the tag has attributes. 通过类属性$ isEmptyElement进行的检测也不起作用,因为标记具有属性。

That's simply wrong. 那是完全错误的。 An empty element with attributes is still empty and $isEmptyElement will reflect that. 具有属性的空元素仍然为空, $isEmptyElement将反映出来。 The problem with your code is that you test $isEmptyElement after moving to the attributes. 您的代码的问题是,移至属性后,您将测试$isEmptyElement 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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM