XML reading with PHP

Associate
Joined
4 Apr 2011
Posts
259
Hi. I'm having trouble trying to read a XML file, which can be found here.

What I'm trying to to is to get the "current_value" of either Hidro1 or Hidro2, depening on a variable, which will be provided by the user. But I really don't know to to do this.
 
I don't have PHP installed here to check so this is untested, but something like:

<?php
$xml = simplexml_load_file("yourfile.xml");

$xml->registerXPathNamespace("ee", "http://www.eeml.org/xsd/0.5.1");

$result = $xml->xpath("//ee:data[@id='Hidro1']/ee:current_value/text()");

print_r($result);
?>

You'll just need a condition in there based on the user's variable/value, and to go with Hidro1 or Hidro2 in the xpath string as appropriate. If the XML is in a string variable rather than a file, you can use simplexml_load_string() instead of simplexml_load_file().

More info and examples of the functions:
http://www.w3schools.com/php/php_ref_simplexml.asp
 
Back
Top Bottom