How can i read the first XML comment in PHP?

送分小仙女□ 提交于 2020-01-07 02:16:30

问题


How can I read the first and only comment in PHP:

<?xml version="1.0" encoding="UTF-8"?>
<!-- read this -->
<root>
</root>

using DOMDOcument object?

$xml = new DOMDocument();

$libxml_error = 'Il file %s non risulta ben formato (%s).';

// Per evitare i warning nel caso di xml non ben formato occorre sopprimere
// gli errori
if (!@$xml -> load($xml_file_path)) $error = sprintf($libxml_error,
    $xml_file_path, libxml_get_last_error()->message);

// Read the fist comment in xml file

回答1:


What about this:

$xpath = new DOMXPath($xml);
foreach ($xpath->query('//comment()') as $comment) {
    var_dump($comment->textContent);
}

Since you probably only want the first one:

$xpath = new DOMXPath($xml);
$results = $xpath->query('//comment()');
$comment = $results[0];
var_dump($comment);

Source: How to retrieve comments from within an XML Document in PHP




回答2:


How to get the comments from XML file - look for instructions here:

How to retrieve comments from within an XML Document in PHP



来源:https://stackoverflow.com/questions/7548043/how-can-i-read-the-first-xml-comment-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!