XML manipulation with PHP

Asked

Viewed 228 times

0

I am creating a tool for reading XML files that will be searched locally in the user’s machine, I searched and found very easy methods to use in PHP, simpleXML.

I have the following code:

<?php 


    $arq = simplexml_load_file('29190411412201000112650010000000181000000180-nfe.xml') or die("Erro ao carregar arquivo XML");


    foreach ($arq->children() as $infNFe) {

        echo $infNFe->natOp;

        echo $infNFe->mod;

        echo $infNFe->tpNF;

        echo $infNFe->dhEmi;
    }

?>

It should return the values of these XML tags, but nothing happens on the page and if I give a var_dump in the variable that receives XML it is returned.

The XML I’m using is the 65 Nfce Model Sales.

I’ll leave the beginning of XML:

<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe versao="4.00" Id="NFe29190411412201000112650010000000181000000180">
<ide>
<cUF>29</cUF>
<cNF>00000018</cNF>
<natOp>VENDA AO CONSUMIDOR</natOp>
<mod>65</mod>
<serie>1</serie>
<nNF>18</nNF>
<dhEmi>2019-04-06T14:29:36-03:00</dhEmi>
<tpNF>1</tpNF>
<idDest>1</idDest>
<cMunFG>2901007</cMunFG>
<tpImp>4</tpImp>
<tpEmis>1</tpEmis>
<cDV>0</cDV>
<tpAmb>1</tpAmb>
<finNFe>1</finNFe>
<indFinal>1</indFinal>
<indPres>1</indPres>
<procEmi>0</procEmi>
<verProc>BahiaNFCe</verProc>
</ide>
<emit>
<CNPJ>11412201000112</CNPJ>
<xNome>D. D. SILVA CALISTO-ME</xNome>
<xFant>AUTO PECAS BOM CONSELHO</xFant>
<enderEmit>
<xLgr>AV. ABERLADO VELOSO</xLgr>
<nro>534</nro>
<xCpl>TERREO</xCpl>
<xBairro>CENTRO</xBairro>
<cMun>2901007</cMun>
<xMun>AMARGOSA</xMun>
<UF>BA</UF>
<CEP>45300000</CEP>
<cPais>1058</cPais>
<xPais>BRASIL</xPais>
</enderEmit>

1 answer

0


Gabriel the problem is that you’re looking for the information at the wrong level, this information is actually a few levels further in. Try it that way:

foreach ($arq->NFe->infNFe->children() as $infNFe) {
    echo $infNFe->natOp;

    echo $infNFe->mod;

    echo $infNFe->tpNF;

    echo $infNFe->dhEmi;
}

Browser other questions tagged

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