Read xml file without knowing tag name

Asked

Viewed 275 times

0

Good morning my friend,

I need to read an xml file (NF-e) where they have several items, but in each item there can be a different tag. In this case it would be in the tax tags, where the ICMS tag can have several names (ICMS00, ICMS02, ICMS100.), and I need to read the tags they have inside.

Example of XML: https://pastebin.com/bXr9pU3C

The code I’m using and trying to make this reading is this:

$link = "importacoes/1-42180984431352000191550010001174881191386794.xml"; //link do arquivo xml

$xml = simplexml_load_file($link);

echo $xml->NFe->infNFe->ide->cUF;

foreach($xml->NFe->infNFe->det as $itemm){
    echo '<br>';echo '<br>';
    echo $itemm->prod->cProd;
    echo '<br>';
    echo $itemm->prod->xProd;
    echo '<br>';
    $icms = $itemm->imposto->ICMS;

    print_r($icms);
    echo (string)$icms->ICMS;
}
  • 1

    There are functions, such as Domdocument::hasChildNodes, which displays nodes that have children, etc... Moreover, all nodes are eternal. You can view the following link: https://3v4l.org/hhWdo

  • Although this is not the answer to the question, it also has a lot of content (and links) about: https://answall.com/a/274577/5007

  • Thanks Gabriel, I’ll study about it, I believe I find there what I need, thanks friend, good week!

No answers

Browser other questions tagged

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