xpath
在 XML 数据上运行 XPath 查询。
simplexml_load_string
PHP 5.x及以上
该函数用于加载一个XML字符串并返回一个 SimpleXMLElement 对象,允许用户在该对象上使用XPath查询来操作和查找XML内容。
simplexml_load_string(string $data, string|null $class = null, int $options = 0, string $ns = "", bool $is_prefix = false): SimpleXMLElement|false
返回一个 SimpleXMLElement 对象,或者在解析失败时返回 false。
$xml_string = '<root><book><title>PHP学习</title><author>张三</author></book></root>'; $xml = simplexml_load_string($xml_string); <p>// 使用XPath查询<br> $result = $xml->xpath('//book/title');<br> echo $result[0]; // 输出:PHP学习<br>
在这个示例中,我们首先定义一个简单的XML字符串,然后使用simplexml_load_string()函数将其解析为SimpleXMLElement对象。接着使用xpath()方法来查询book节点下的title元素,最终输出查询到的内容。