Leitura arquivo XML de NFe

09/12/2009

0

Alguém tem ideia qual o melhor método para abrir, e importar registros dum arquivo XML de NFe? 
Foi utilizado lendo direto do arquivo para o CDS ocorrendo  erro (Mismatch in datapacket);
Tentei conf. utilizando o TXMLTransformProvider porém erro (TransformationFile must be specified);
OBS: algumas <tags> não são reconhecidas, por isso não estou querendo reinventar a roda.
PS: Não importa o B.Dados porque são isso é para vários...(Oracle, SQLServer, PostgreSQL DB2 Etc.)
Jair N.

Jair N.

Responder

Posts

11/12/2009

Jair N.

Esperava uma solução melhor pois alguns itens referenciada na nota fiscal ocorre erro por este método:

procedure btnImpNFeClick(Sender: TObject);
var
  xmlNCab, xmlNItm : IXMLNode;
  widstrRetorno    : WideString;
  xmldoc_nfe       : TXMLDocument;
  opndlg_nfe       : TOpenDialog;

  Function funcvarXML(xmlNTag : IXMLNode): WideString;
  Begin
    If Not (xmlNTag.ChildNodes.First = Nil) Then
      Result := xmlNTag.ChildNodes.First.Text;
  End;

begin
  xmldoc_nfe := TXMLDocument.Create(Self);
  opndlg_nfe := TOpenDialog.Create(Self);

  //  *** Importar Arquivo XML NFe...
  opndlg_nfe.InitialDir := Application.GetNamePath;
  opndlg_nfe.Filter := 'NFe|*.XML';
  opndlg_nfe.Title  := 'Selecione o arquivo a importar';
  opndlg_nfe.Execute;

  If Not (opndlg_nfe.FileName = '') Then Exit;
  // Lendo o arquivo pelas TAGs...
  xmldoc_nfe.LoadFromFile(opndlg_nfe.FileName);
  // Leitura Dados da Nota Fiscal...
  xmlNCab := xmldoc_nfe.DocumentElement.childNodes.First.ChildNodes.FindNode('ide');
  If Not (xmlNCab.ChildNodes.First = Nil) Then
  Begin
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['cNF'    ]); // Código sequencial NFe
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['nNF'    ]); // Número da Nota Fiscal
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['dEmi'   ]); // Data Emissão Nota Fiscal
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['verProc']); // Descrição da Nota Fiscal
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['serie'  ]); // Série da Nota Fiscal
  End;
  // Leitura Dados do Fornecedor...
  xmlNCab := xmldoc_nfe.DocumentElement.childNodes.First.ChildNodes.FindNode('emit');
  If Not (xmlNCab.ChildNodes.First = Nil) Then
  Begin
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['CNPJ'   ]); // CGC Fornecedor
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['CPF'    ]); // CPF Fornecedor
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['xNome'  ]); // Razão Social do Fornecedor
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['xFant'  ]); // Nome Fantasia
    widstrRetorno := funcvarXML(xmlNCab.ChildNodes['IE'     ]); // Inscrição Estadual
    // Leitura Dados Endereços do Fornecedor...
    xmlNItm := xmlNCab.ChildNodes['enderEmit'];
    If Not (xmlNItm.ChildNodes.First = Nil) Then
    Begin
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['xLgr'   ]); // Logradouro
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['nro'    ]); // Número
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['xCpl'   ]); // Complemento
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['xBairro']); // Bairro
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['cMun'   ]); // Código Município IBGE
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['xMun'   ]); // Nome Município
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['UF'     ]); // Unidade Federação
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['CEP'    ]); // CEP
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['cPais'  ]); // Código Pais BACEN
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['xPais'  ]); // Nome Pais
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['fone'   ]); // Telefone
    End;
  End;
  // Leitura Dados Totais da Nota Fiscal...
  xmlNCab := xmldoc_nfe.DocumentElement.childNodes.First.ChildNodes.FindNode('total');
  If Not (xmlNCab.ChildNodes.First = Nil) Then
  Begin
    xmlNItm := xmlNCab.ChildNodes['ICMSTot'];
    If Not (xmlNItm.ChildNodes.First = Nil) Then
    Begin
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['vNF'    ]); // Valor total da Nota Fiscal(2dec,S/M)
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['vProd'  ]); // Valor total da Nota Fiscal(2dec,S/M)
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['vDesc'  ]); // Valor total dos descontos (2dec,S/M)
    End;
  End;
  // Leitura Dados Itens da Nota Fiscal...
  xmlNCab := xmldoc_nfe.DocumentElement.childNodes.First.ChildNodes.FindNode('det');
  While Not (xmlNCab = Nil) Do
  Begin
    xmlNCab.ChildNodes.First.ChildNodes.FindNode('det');
    xmlNItm := xmlNCab.ChildNodes['prod'];
    If Not (xmlNItm.ChildNodes.First = Nil) Then
    Begin
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['cProd'  ]); // Código do produto CFOP
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['xProd'  ]); // Nome do produto
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['uCom'   ]); // Sigla unidade da embalagem
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['qCom'   ]); // Quantidade do produto (4dec,S/M)
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['vUnCom' ]); // Valor unitário do produto (4dec,S/M)
      widstrRetorno := funcvarXML(xmlNItm.ChildNodes['vProd'  ]); // Valor total do produto (2dec,S/M)
    End;
    xmlNCab := xmlNCab.NextSibling;
  End;

end;

Responder

11/12/2009

Ribamar Santos

  ***nao sei se é a melhor forma, mas ...***   .....        XMLDocument1.LoadFromFile(listbox2.Items[P]);
       for i:=0 to XMLDocument1.DocumentElement.ChildNodes.Count -1 do
           for x:=0 to XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes.Count -1 do
               for z:=0 to XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes.Count -1 do
                  begin
                    for y:=0 to XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes.Count -1 do
                      begin
                        cuf:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].NodeName;
                        if cuf='infAdic' then
                           cuf:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeName;
                           if cuf='obsCont' then
                              cuf:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].ChildNodes[0].NodeName;
                              if cuf='xTexto' then
                                  SAIDASEMAIL_DEST.AsString:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].ChildNodes[0].NodeValue;
                        cuf:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeName;
                        if cuf='natOp' then //*** NATUREZA ***
                            SAIDASNATUREZA.AsString:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeValue;
                        if cuf='nNF' then //*** NUMERO DA NFF ***
                            SAIDASNUME_NFF.AsString:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeValue;
                        If cuf='xNome' then //*** RAZAO destinatario***
                            SAIDASRAZAO_DEST.AsString:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeValue;
                        if cuf='dEmi' then //*** DATA DA EMISSAO
                            //SAIDASEMIS_NFF.AsDateTime:=
                            //cuf:=DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeValue;
                            //cuf:=strto
                            //SAIDASEMAIL_DEST.AsString:=XMLDocument1.DocumentElement.ChildNodes[i].ChildNodes[x].ChildNodes[z].ChildNodes[y].NodeValue;
                      end;
                  end;
       SAIDAS.Post;   ..espero que ajude..
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar