Problem-Reading XML with PHP

Asked

Viewed 54 times

-1

I have the following XML:

<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe Id="NFe35201127619821000167550010000026321100027970" versao="4.00">
<ide>
...
</ide>
<emit>
...
</emit>
<dest>
...
</dest>
<det nItem="1">
<prod>
<cProd>9-7071/669</cProd>
<cEAN>7892787019411</cEAN>
<xProd>ALTO-FALANTE KIT FACIL-CEST:0105700</xProd>
<NCM>85182990</NCM>
<CEST>0105700</CEST>
<cBenef/>
<CFOP>5405</CFOP>
<uCom>KT</uCom>
<qCom>20.0000</qCom>
<vUnCom>98.82700000</vUnCom>
<vProd>1976.54</vProd>
<cEANTrib>7892787019411</cEANTrib>
<uTrib>UN</uTrib>
<qTrib>80.0000</qTrib>
<vUnTrib>24.70675000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
...
</imposto>
<infAdProd>Base Calc. ST: R$ 837,75-ICMS Ret. ST: R$ 67,28</infAdProd>
</det>
<det nItem="2">
...
</det>
<det nItem="3">
...
</det>
<total>
...
</total>
<transp>
...
</transp>
<cobr>
...
</cobr>
<pag>
...
</pag>
<infAdic>
...
</infAdic>
<infRespTec>
...
</infRespTec>
</infNFe>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
</Signature>
</NFe>
<protNFe versao="4.00">
...
</protNFe>
</nfeProc>

I called xml, via php as follows:

<html>
  <body>
    <?php
      header("Content-type: text/html; charset=utf-8");
      $xml = simplexml_load_file("C:\Users\Expedição\Downloads\aktion 2632.xml");
      print "<pre>";
      //print_r($xml);
      print "</pre>";
      foreach ($xml as $itens){
        echo $itens->NFe->nItem->cEAN;
      }
    ?>
  </body>
</html>

The problem is that I just want to return me the field cEAN and I’m not getting it.

OBS: updated with integer XML.

The return:

inserir a descrição da imagem aqui

1 answer

1


Guy couldn’t use his xml it gives an error because it’s not closed </ or something like that. So check the structure of your xml file.

<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
  <Test TestId="0001" TestType="CMD">
    <Name>Convert number to string</Name>
    <CommandLine>Examp1.EXE</CommandLine>
    <Input>1</Input>
    <Output>One</Output>
  </Test>
  <Test TestId="0002" TestType="CMD">
    <Name>Find succeeding characters</Name>
    <CommandLine>Examp2.EXE</CommandLine>
    <Input>abc</Input>
    <Output>def</Output>
  </Test>
  <Test TestId="0003" TestType="GUI">
    <Name>Convert multiple numbers to strings</Name>
    <CommandLine>Examp2.EXE /Verbose</CommandLine>
    <Input>123</Input>
    <Output>One Two Three</Output>
  </Test>
  <Test TestId="0004" TestType="GUI">
    <Name>Find correlated key</Name>
    <CommandLine>Examp3.EXE</CommandLine>
    <Input>a1</Input>
    <Output>b1</Output>
  </Test>
</Tests>
<?php
    $xml = simplexml_load_file("C:\Users\Onivaldo\Desktop\xml.xml");
   
    foreach ($xml as $itens){
       echo $itens->Name. PHP_EOL;
    }

I’ll leave this passage for example. I hope it helps.

Edit: I took a look at this link and saw that Voce needs to go all the way to the tag you want. https://www.w3schools.com/php/php_xml_simplexml_get.asp

Your code will look like this:

<?php
      $xml = simplexml_load_file("C:\Users\Onivaldo\Desktop\xml.xml");
        
      foreach ($xml as $itens){
          echo $xml->NFe->infNFe->det->prod->cEAN . PHP_EOL;
      }
  • A is vdd I xml not this full pq is too big I will edit

  • but looking at the example you can see that just directly access $xml->cEAN, this will bring the output you expect, if it does not work post the entire xml and the generated error.

  • unfortunately it did not work more updated with whole xml and error.

  • I changed the answer see if it worked for you

  • It worked really well thank you

Browser other questions tagged

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