1

我有一个由我的 Web 服务生成的 xml 文件,我现在想显示 xml 中的一些信息

xml 中的所有标签都处于同一级别,没有层次结构。

但是,我在文档的两侧都有这些标签,即:

<xml version="1.0" encoding="ISO-8859-1"><xmlresponse>

和:

</xmlresponse></xml>

afin d'utiliser une balise et de l'afficher j'ai donc le code suivant:

<?php
$document_xml = new DomDocument(); 
$resultat_html = '';
$document_xml->load('tmp/'.$_GET['n_doss'].'.xml'); 
//echo $elements = $document_xml->getElementsByTagName('rating');


$title = $document_xml->xpath('rating');
echo trim($title[0]);
?>

但是,我转发一条错误消息说:

Fatal error: Call to undefined method DOMDocument :: xpath ()

我不知道如何解决它。

预先感谢您的帮助。

4

2 回答 2

2

Check your xml file is valid and it loads correctly by DomDocument. And why you don't use SimpleXML library ? It also supports XPath expressions the following way:

$simplexml= new SimpleXMLElement($xml); // loading RAW XML string `$xml` retrieved from WebService
$items =  $simplexml->xpath('/rating'); // querying DOM to retrieve <rating></rating> from the root of of the structure
echo $items[0]; // getting result

NOTE: DomDocument doesn't have xpath method. Use query method to apply XPath expression to the object using DomXPath like this:

$dom = new DomDocument();
$dom->load('tracks.xml');
$xpath = new DOMXPath($dom);
$query = '//distance[. > "15000"]';
$distances = $xpath->query($query);
于 2012-08-02T12:08:17.400 回答
1

I don't think the DOMDocument has an xpath method. Instead you can use the DOMXPath class.

Try this:

$xpath = new DOMXPath($document_xml);
$ratings = $xpath->query('rating');
于 2012-08-02T12:08:20.303 回答