2
I get an XML file for a hosting directory and the file has the following structure:
<categorias>
<id_53>142</id_53>
<id_89>346</id_89>
<id_160>457</id_160>
<id_5498>856</id_5498>
<id_177>98</id_177>
<id_183>22</id_183>
<id_248>34</id_248>
<id_184>782</id_184>
<id_203>35</id_203>
<id_214>743</id_214>
<id_218>325</id_218>
<id_219>678</id_219>
<id_220>393</id_220>
<id_136>177</id_136>
<id_137>274</id_137>
</categorias>
Since I only need to get a specific tag, I tried to do it this way:
<?php
// gero a variável com o conteúdo do XML
$arquivo_xml = simplexml_load_file('xml_impo/cats.xml');
// Imprimo o valor da tag id_220, ou qualquer outra quando necessário
echo $arquivo_xml->categorias[0]->id_220;
##############################################
##### TENTEI TAMBEM / SEM SUCESSO
// gero a variável com o conteúdo do XML
$arquivo_xml= new SimpleXMLElement('xml_impo/cats.xml');
// Imprimo o valor da tag id_220, ou qualquer outra quando necessário
echo $arquivo_xml->categorias[0]->{'id_220'};
?>
But on the screen returns blank.
I always need to take only one value, specifying the echo
id_00 to be received;
Actually the code would work, but it is not necessary to perform a foreach to read a field in the xml root, I explained better in the comment above.
– Vinicius Farias
Yes Vinicius, your answer is wider. The foreach is for cases where we do not know exactly the name of the tags and we need to go through them. I don’t know how to check if a certain tag exists, following your example. It would be something like this?
if (isset($arquivo_xml->id_220)) { echo 'Existe 220'; }
Or has another form?– Rodrigo Tognin
Thank you @Rodrigotognin
– ElvisP