如何阅读PHP中的第一个XML注释? [英] How can i read the first XML comment in PHP?

查看:95
本文介绍了如何阅读PHP中的第一个XML注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

使用DOMDOcument对象?

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


推荐答案

这怎么办:

$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);

来源如何从PHP的XML文档中检索注释

这篇关于如何阅读PHP中的第一个XML注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆