Simple XML parsing on PHP
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 cod开发者_开发知识库e="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 :)
$code = isset($_GET['code']) ? $_GET['code'] : 0;
$xml = simplexml_load_file('.....file.xml');
foreach ($xml->clima as $item)
{
if ($code == (int)$item['code'])
{
echo 'Found, code' . $item['code'] . ' value: '. $item['value'] . '<br />';
break;
}
}
edit, example:
<?php
$str='<?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>
';
$str = utf8_encode($str);
$code = isset($_GET['code']) ? $_GET['code'] : 0;
$xml = simplexml_load_string($str);
foreach ($xml->clima as $item)
{
if ($code == (int)$item['code'])
{
echo 'Found, code' . $item['code'] . ' value: '. $item['value'] . '<br />';
break;
}
}
Here's an example that I tested, using XSLTProcessor:
<?php
$xsl = new DOMDocument();
$xsl->loadXML('<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="code" />
<xsl:template match="/">
<xsl:for-each select="ES/clima[@code=$code]">
<xsl:value-of select="@value"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>');
$xml = new DOMDocument();
$xml->load("data.xml"); // this is the name of your XML data file
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$code = 19; // or whatever code you're looking for
$proc->setParameter('', 'code', $code);
echo $proc->transformToXML($xml);
Output:
Tormenta de tierra
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.
- XSLProcessor (@Bill Karwin's solution): 5.74sec
- SimpleXML with XPath (@VolkerK's solution): 3.94sec
- SimpleXML without XPath (@Kieran Allen's solution): 3.91sec
- fgets/sscanf (@John at CashCommons' solution): 3.45sec
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
$doc = simplexml_load_string('...your xml document here...');
foreach( $doc->xpath('/ES/clima[@code="19"]') as $clima ) {
echo $clima['value'], "\n";
}
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 next value
. Something like:
$n = sscanf($line, "<clima code=\"%d\" value=\"$s\" />", $code, $value);
(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.
精彩评论