How to retrieve comments from within an XML Document in PHP

后端 未结 4 1735
走了就别回头了
走了就别回头了 2020-12-04 03:04

I want to extract all comments below a specific node within an XML document, using PHP. I have tried both the SimpleXML and DOMDocument methods, but I keep getting blank out

4条回答
  •  醉酒成梦
    2020-12-04 03:44

    Use XMLReader. Comments can be easily detected/found, they are xml elements of type COMMENT. For details see PHP documentation: The XMLReader class

    Code example:

    $reader = new XMLReader();
    $reader->open('filename.xml');
    while ($reader->read()){
        if ($reader->nodeType == XMLReader::COMMENT) {
            $comments[] = $reader->readOuterXml();
        }
    }
    

    And in array $comments you will have all comments found in XML file.

提交回复
热议问题