Guys i have a xml file who contain this info:
<?xml version="1.0" encoding="utf-8"?>
<ES>
<clima code="0" value="Tornado" />
<clima code="1" value="Tormenta tropical" />
<clima code="2" value="Huracan" />
<clima code="3" value="Tormentas severas" />
<clima code="4" value="Tormentas eléctricas" />
<clima code="5" value="Mixtos de lluvia y nieve" />
<clima code="6" value="Mixtos de lluvia y aguanieve" />
<clima code="7" value="Mixto de nieve y aguanieve" />
<clima code="8" value="Congelación llovizna" />
<clima code="9" value="Llovizna" />
<clima code="10" value="Lluvia helada" />
<clima code="11" value="Chubascos" />
<clima code="12" value="Chubascos" />
<clima code="13" value="Nieve" />
<clima code="14" value="Nieve, Chubascos" />
<clima code="15" value="Nieve, ventoso" />
<clima code="16" value="Nieve" />
<clima code="17" value="Granizo" />
<clima code="18" value="Aguanieve" />
<clima code="19" value="Tormenta de tierra" />
<clima code="20" value="Niebla" />
<clima code="21" value="Haze" />
<clima code="22" value="Humo" />
<clima code="23" value="blustery" />
<clima code="24" value="Viento" />
<clima code="25" value="Frío" />
<clima code="26" value="Nublado" />
<clima code="27" value="Chubascos por la noche" />
<clima code="28" value="Día parcialmente nublado" />
<clima code="29" value="Noche Parcialmente nublado" />
<clima code="30" value="Día Parcialmente nublado" />
<clima code="31" value="Noche Despejada" />
<clima code="32" value="Soleado" />
<clima code="33" value="Noche despejada" />
<clima code="34" value="Día despejado" />
<clima code="35" value="mixtos de lluvia y granizo" />
<clima code="36" value="Caluroso" />
<clima code="37" value="Tormentas aisladas" />
<clima code="38" value="Tormentas aisladas" />
<clima code="39" value="Tormentas aisladas" />
<clima code="40" value="Chaparrones" />
<clima code="41" value="Nieve" />
<clima code="42" value="Tormenta de nieve dispersos" />
<clima code="43" value="Nieve" />
<clima code="44" value="Parcialmente nublado" />
<clima code="45" value="Tormentas" />
<clima code="46" value="snow showers" />
<clima code="47" value="Tormentas aisladas" />
<clima code="3200" value="Sin datos" />
</ES>
Im very new at php, can you show me how given a code return the value? Please without any fancy classes :)
If your XML is as well formatted as that, you could just load the file in line by line and search for the proper
code
starting at the beginning of the file, and then return the nextvalue
. Something like:(Completely untested but you get the idea I hope. Just loop through the lines until you get the right
$code
then$value
has the match.)But I agree with Kerry that classes would be far easier (like http://php.net/manual/en/book.xml.php for example) and would take care of more contingencies in your XML.
Here's an example that I tested, using XSLTProcessor:
Output:
I'm sorry that this uses classes, but it works. You don't have to write any loops in PHP. It just returns the value you're looking for.
You can also store the XSL stylesheet in a file and load it by filename, instead of the example I showed loading it by literal string.
Re comment from @VolkerK: I tried timing these three solutions, but running them 10,000 times in a PHP CLI script and using
microtime(true)
before and after.edit, example:
You can use XPath to find/select one or more specific elements in a document.
E.g. PHP's DOM extension exposes a XPath interface as well as SimpleXML