1

I have an xml file generated by my web service, I now want to display some information from the xml

all tags in the xml are in the same level, there is no hierarchy.

However, I have these tags on either side of the document, namely:

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

And:

</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]);
?>

However, I am forwarding an error message saying:

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

I do not know how to solve it.

Thank you in advance for your help.

2 Answers 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);
Sign up to request clarification or add additional context in comments.

9 Comments

I just would like to display some elements from the xml file. xml file is valid because it is generated by my web service
@StanislasPiotrowski, use DOMXPath to apply XPath expressions to already loaded DomDocument.
now it says: Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects parameter 1 to be string, object given' in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\DOSSIERS\creditsafe.php on line 31 ( ! ) Exception: SimpleXMLElement::__construct() expects parameter 1 to be string, object given in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\DOSSIERS\creditsafe.php on line 31
I've written this <?php $document_xml = new DomDocument(); $resultat_html = ''; $document_xml->load('tmp/'.$_GET['n_doss'].'.xml'); //echo $elements = $document_xml->getElementsByTagName('rating'); $simplexml= new SimpleXMLElement($document_xml); // loading XML to the object $items = $simplexml->xpath('/rating'); // querying DOM echo $items[0]; ?>
@StanislasPiotrowski, $xml transferred to the SimpleXMLElement should be RAW XML string, not an object.
|
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');

1 Comment

niw it says to me Catchable fatal error: Object of class DOMNodeList could not be converted to string in C

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.