问题
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