Problem with Reading XML NF-e det tag

Asked

Viewed 40 times

0

I’m having trouble reading the tag <det> to collect taxes.

Use Delphi 5 with class MSXML2_TLB to read the XML.

Only missing knowledge to finalize this part of the routine I’m having trouble reading the tax tags. How can I do it?

function TForm1.F_RetXMLDetItem(aArquivo: String; aNotaFiscal: TNotaFiscal; aNotaFiscalDet: TItemNF): boolean;
var fDocXML: IXMLDOMDocument;
    fNoItens, fnoProduto, fAttrib, fAttrib2: IXMLDOMNode;
    fLSItens, fsubLSItem: IXMLDOMNodeList;
    i, j, iItem: integer;
    s, s1: string;
begin
    fDocXML := CoDOMDocument.Create;
    try
        fDocXML.load(aArquivo);
        fDocXML.async := False;

        fLSItens := fDocXML.getElementsByTagName('det');

        repeat
          fNoItens := fLSItens.nextNode;
          //
          if (fNoItens = nil) then
             break;

          Memo1.Lines.Add('');
          Memo1.Lines.Add('[ ' + UpperCase(fNoItens.baseName) + ' ]');
          Memo1.Lines.Add('');
          //
          iItem := 0;
          // Total de Itens na nota
          for I := 0 to fNoItens.childNodes.length -1 do        
          begin
              iItem := I;
              if iItem <> I then
              begin
                  aNotaFiscal.ItensNF.Add(aNotaFiscal, aNotaFiscalDet);
              end
              else
              begin
                  fAttrib := fNoItens.attributes.getNamedItem('nItem');
                  s := fAttrib.baseName + ' - ' + fNoItens.baseName;
                  //
                  Memo1.Lines.Add('ITEM: ' + fAttrib.nodeName + ' - ' + fAttrib.nodeValue);
                  if fNoItens.childNodes.item[i].nodeName = 'prod' then
                  begin
                      fsubLSItem := fLSItens.item[i].Get_childNodes;
                      fnoProduto := fsubLSItem.nextNode;
                      if (fnoProduto = nil) then
                          break;
                      // Itens da Nota
                      for J := 0 to fnoProduto.childNodes.length -1 do                         
                      begin
                          //fAttrib2 := fnodeFilho2.attributes.getNamedItem(fNodeFilho.childNodes.item[i].nodeName);
                          // s1 := fNodeFilho.childNodes.item[j].nodeName + ' prod ' + fnodeFilho2.baseName + ' prod ';
                          //s1 := fNodeFilho.childNodes.item[j].nodeName + ' - ' + fNodeFilho.attributes[j].nodeName;
                          //Memo1.Lines.Add(fNodeFilho2.childNodes.item[j].nodeName);  // cprod
                          //Memo1.Lines.Add(fNodeFilho2.childNodes.item[j].baseName);  // cprod
                          //Memo1.Lines.Add(fNodeFilho2.childNodes.item[j].text);  // element
                          //Memo1.Lines.Add(fNodeFilho2.childNodes.item[j].xml);  // prod
//                          Memo1.Lines.Add(fNodeFilho2.childNodes.item[j].attributes[i].nodeName);  // prod

                          (* Itens do Produto *)
                          Memo1.Lines.Add('[' + fnoProduto.childNodes.item[j].nodeName + '] ' + fnoProduto.childNodes.item[j].text);
                          if j+1 >= fnoProduto.childNodes.length then
                          begin
                              Memo1.Lines.Add('[' + fNoItens.childNodes.item[1].nodeName + '] ' + '');  // Impostos
                              Memo1.Lines.Add('[' + fNoItens.childNodes.item[2].nodeName + '] ' + '');  // infAdProd
                          end;
                      end;
                      //if fNodeFilho.childNodes.item[i].nodeName <> 'prod' then
                      //   Memo1.Lines.Add('[' + fNodeFilho.childNodes.item[i].nodeName + '] ' + fNodeFilho.childNodes.item[i].text);
                      // Memo1.Lines.Add('[' + fNodeFilho.childNodes.item[i].nodeName + '] ' + fNodeFilho.childNodes.item[i].text);
//                      Result := F_RetXMLNFProd(aArquivo, aNotaFiscalDet);  Apagar essa função
//                      Result := F_RetXMLNFImposto(aNode, aNotaFiscalDet);
                  end;
                  s1 := fNoItens.childNodes.item[i].nodeName + ' - ' + fnoProduto.baseName + ' prod ';
//                  else if fNodeFilho.childNodes.item[i].nodeName = 'imposto' then
                  begin
                  end;
              end;
    //              if fNodeFilho.childNodes.item[i].nodeName = 'item' then
    //                 aNotaFiscalDet.Item := StrToInt(fNodeFilho.childNodes.item[i].text)
          end;
        until (fNoItens = nil);
    finally
        fDocXML := nil;
    end;
end;

Here’s the XML snippet that I can’t import: that should be Items + taxes.

-<det nItem="1">
  -<prod>
    <cProd>201342</cProd>
    <cEAN>SEM GTIN</cEAN>
    <xProd>Vit E 50% (Acetato de Tocoferol) (MI/IMP)</xProd>
    <NCM>29362812</NCM>
    <CFOP>6102</CFOP>
    <uCom>KG</uCom>
    <qCom>300.0000</qCom>
    <vUnCom>46.4849000000</vUnCom>
    <vProd>13945.47</vProd>
    <cEANTrib>SEM GTIN</cEANTrib>
    <uTrib>KG</uTrib>
    <qTrib>300.0000</qTrib>
    <vUnTrib>46.4849000000</vUnTrib>
    <indTot>1</indTot>
    <nItemPed>1</nItemPed>
  </prod>
  -<imposto>
    <vTotTrib>3960.51</vTotTrib>
  -<ICMS>
    -<ICMS20>
      <orig>7</orig>
      <CST>20</CST>
      <modBC>3</modBC>
      <pRedBC>60.0000</pRedBC>
      <vBC>5578.19</vBC>
      <pICMS>12.0000</pICMS>
      <vICMS>669.38</vICMS>
    </ICMS20>
  </ICMS>
  -<IPI>
    <cEnq>999</cEnq>
    -<IPINT>
      <CST>53</CST>
    </IPINT>
  </IPI>
  -<PIS>
    -<PISNT>
      <CST>06</CST>
    </PISNT>
  </PIS>
  -<COFINS>
    -<COFINSNT>
      <CST>06</CST>
    </COFINSNT>
  </COFINS>
 </imposto>
  <infAdProd>;Serie/Lote/Quantidade/Validade: 00001831, UT20050157, 300, 10/05/2024</infAdProd>
</det>

-<det nItem="2">
  -<prod>
    <cProd>201332</cProd>
    <cEAN>SEM GTIN</cEAN>
    <xProd>Vit D3 (500 UI/mg) (MI/IMP)</xProd>
    <NCM>23099090</NCM>
    <CFOP>6102</CFOP>
    <uCom>KG</uCom>
    <qCom>20.0000</qCom>
    <vUnCom>181.4594000000</vUnCom>
    <vProd>3629.19</vProd>
    <cEANTrib>SEM GTIN</cEANTrib>
    <uTrib>KG</uTrib>
    <qTrib>20.0000</qTrib>
    <vUnTrib>181.4594000000</vUnTrib>
    <indTot>1</indTot>
    <nItemPed>2</nItemPed>
  </prod>
  -<imposto>
    <vTotTrib>646.36</vTotTrib>
  -<ICMS>
    -<ICMS00>
      <orig>2</orig>
      <CST>00</CST>
      <modBC>3</modBC>
      <vBC>3629.19</vBC>
      <pICMS>4.0000</pICMS>
      <vICMS>145.17</vICMS>
    </ICMS00>
  </ICMS>
  -<IPI>
    <cEnq>999</cEnq>
    -<IPINT>
      <CST>53</CST>
    </IPINT>
  </IPI>
  -<PIS>
    -<PISAliq>
      <CST>01</CST>
      <vBC>3629.19</vBC>
      <pPIS>1.6500</pPIS>
      <vPIS>59.88</vPIS>
    </PISAliq>
  </PIS>
  -<COFINS>
    -<COFINSAliq>
      <CST>01</CST>
      <vBC>3629.19</vBC>
      <pCOFINS>7.6000</pCOFINS>
      <vCOFINS>275.82</vCOFINS>
    </COFINSAliq>
  </COFINS>
</imposto>
<infAdProd>;Serie/Lote/Quantidade/Validade: 00001749, UE02005017, 20, 11/05/2022</infAdProd>
</det>























<infAdProd>;Serie/Lote/Quantidade/Validade: 00001810, DTP2003-99C, 500, 27/03/2023</infAdProd>

</det>
No answers

Browser other questions tagged

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