我如何在PHP中读取第一个XML注释


How can i read the first XML comment in PHP?

如何在PHP中读取第一个也是唯一的注释:

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

使用DOMDOcument对象?

$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

这个呢:

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

因为你可能只需要第一个:

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

Source:如何在PHP中检索XML文档中的注释

如何从XML文件中获取注释-查看此处的说明:

如何在PHP中检索XML文档中的注释