Get specific XML tag with PHP

Asked

Viewed 843 times

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;

2 answers

3


When you load the files via Simplexmlelement or simplexml_load_file it already enters the xml root, ie the index categories is already the default index, to catch the default id you only need to execute the following command:

echo $arquivo_xml->id_220

If in the future you have several categories the right one to do in xml would be:

<lista>
    <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>
    <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>
</lista>

With this XML above it would be necessary to position the array, as exemplified in the question, because the default index has become the list:

echo $arquivo_xml->categorias[0]->id_220;

0

Eliseu, you can upload the file and read it on a foreach, this way:

<?php
   $arquivo_xml = simplexml_load_file('xml1.xml');

   foreach($arquivo_xml as $tag => $val){
      echo $tag . ' = ' . $val . '<br>';

      // Se precisar verificar alguma tag específica:
      if ($tag == 'id_220'){ echo 'TAG 220, valor: ' . $val . '<br>'; }
   }
?>
  • 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.

  • 2

    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?

  • Thank you @Rodrigotognin

Browser other questions tagged

You are not signed in. Login or sign up in order to post.