Fórum Como faço para ler essas tags do xml ? #584577
08/08/2017
0
<deducoes>
<deducao codigo="CSLL">32.13</deducao>
<deducao codigo="IRRF">48.20</deducao>
<deducao codigo="COFINS">96.39</deducao>
<deducao codigo="PIS">20.88</deducao>
</deducoes>preciso ler essas tags porém não estou conseguindo segue a forma de leitura :
VIr := copy(ArquivoXML.Text,Pos('<deducao codigo="IRRF">', ArquivoXML.Text)+4,
Pos('<deducao codigo="IRRF">', ArquivoXML.Text)-Pos('<deducao codigo="IRRF">', ArquivoXML.Text)+4);Rodrigo Oliveira
Curtir tópico
+ 0Posts
08/08/2017
Romario Kilian
VIr := copy(ArquivoXML.Text,Pos(''<deducao codigo="IRRF">'', ArquivoXML.Text)+5 <---- tamanho =5 devido ao ponto decimal ??
Pos(''<deducao codigo="IRRF">'', ArquivoXML.Text)-Pos(''<deducao codigo="IRRF">'', ArquivoXML.Text)+5);
e na ultima expressão acho que seria -Pos(''/dedução ao invés de <dedução).
Se falei bobagem ignore..
Gostei + 0
08/08/2017
Rodrigo Oliveira
Gostei + 0
09/08/2017
Natanael Ferreira
Vamos supor que você tenha estas tags em um memo chamado ArquivoXML.
O código para buscar o valor do IRRF ficaria assim:
var
VIr: string;
I, inicio, fim: Integer;
begin
for I := 0 to ArquivoXML.Lines.Count do
if pos('IRRF', ArquivoXML.Lines[I]) > 0 then
begin
inicio := pos('IRRF', ArquivoXML.Lines[I]) + 6;
fim := pos('</deducao', ArquivoXML.Lines[I]);
VIr := copy(ArquivoXML.Lines[I], inicio, fim - inicio);
end;
ShowMessage(VIr);
end;Gostei + 0
09/08/2017
Rodrigo Oliveira
Gostei + 0
09/08/2017
Rodrigo Oliveira
Gostei + 0
09/08/2017
Natanael Ferreira
var
VIr: string;
I, inicio, fim: Integer;
ArquivoXML: TStringList;
begin
ArquivoXML := TStringList.Create;
try
with ArquivoXML do
begin
Add('<deducoes>');
Add(' <deducao codigo="CSLL">32.13</deducao>');
Add(' <deducao codigo="IRRF">48.20</deducao>');
Add(' <deducao codigo="COFINS">96.39</deducao>');
Add(' <deducao codigo="PIS">20.88</deducao>');
Add('</deducoes>');
end;
for I := 0 to ArquivoXML.Count - 1 do
if pos('IRRF', ArquivoXML[I]) > 0 then
begin
inicio := pos('IRRF', ArquivoXML[I]) + 6;
fim := pos('</deducao', ArquivoXML[I]);
VIr := copy(ArquivoXML[I], inicio, fim - inicio);
end;
ShowMessage(VIr);
finally
FreeAndNil(ArquivoXML);
end;
end;Gostei + 0
09/08/2017
Rodrigo Oliveira
Gostei + 0
09/08/2017
Natanael Ferreira
Testa aí:
var
VIr, VPis, VCsll, VCofins: string;
I, inicio, fim: Integer;
ArquivoXML: TStringList;
begin
ArquivoXML := TStringList.Create;
try
with ArquivoXML do
begin
Add('<deducoes>');
Add(' <deducao codigo="CSLL">11132.13</deducao>');
Add(' <deducao codigo="IRRF">11148.20</deducao>');
Add(' <deducao codigo="COFINS">1196.39</deducao>');
Add(' <deducao codigo="PIS">1120.88</deducao>');
Add('</deducoes>');
end;
for I := 0 to ArquivoXML.Count - 1 do
begin
if pos('CSLL', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VCsll := copy(ArquivoXML[I], inicio, fim - inicio);
end;
if pos('IRRF', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VIr := copy(ArquivoXML[I], inicio, fim - inicio);
end;
if pos('COFINS', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VCofins := copy(ArquivoXML[I], inicio, fim - inicio);
end;
if pos('PIS', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VPis := copy(ArquivoXML[I], inicio, fim - inicio);
end;
end;
ShowMessage('CSLL: ' + VCsll);
ShowMessage('IRRF: ' + VIr);
ShowMessage('COFINS: ' + VCofins);
ShowMessage('PIS: ' + VPis);
finally
FreeAndNil(ArquivoXML);
end;
end;Gostei + 0
09/08/2017
Rodrigo Oliveira
Gostei + 0
09/08/2017
Natanael Ferreira
Se restar dúvidas, pode perguntar novamente.
var
VIr, VPis, VCsll, VCofins: string;
I, inicio, fim: Integer;
ArquivoXML: TStringList;
begin
// Criando o Stringlist
ArquivoXML := TStringList.Create;
try
// Populando o XML
with ArquivoXML do
begin
Add('<deducoes>');
Add(' <deducao codigo="CSLL">11132.13</deducao>');
Add(' <deducao codigo="IRRF">11148.20</deducao>');
Add(' <deducao codigo="COFINS">1196.39</deducao>');
Add(' <deducao codigo="PIS">1120.88</deducao>');
Add('</deducoes>');
end;
// Percorrendo o StringList linha a linha
for I := 0 to ArquivoXML.Count - 1 do
begin
// Pos serve para tentar encontrar uma substring(CSLL, neste caso) na linha
if pos('CSLL', ArquivoXML[I]) > 0 then
begin
// Pos novamente para encontrar o índice do início do valor
inicio := pos('">', ArquivoXML[I]) + 2;
// Pos novamente para encontrar o índice do fim do valor
fim := pos('</deducao', ArquivoXML[I]);
// Usamos o copy para copiar o valor utilizando os índices encontrados anteriormente
VCsll := copy(ArquivoXML[I], inicio, fim - inicio);
end;
// As linha abaixo seguem o mesmo modelo
if pos('IRRF', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VIr := copy(ArquivoXML[I], inicio, fim - inicio);
end;
if pos('COFINS', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VCofins := copy(ArquivoXML[I], inicio, fim - inicio);
end;
if pos('PIS', ArquivoXML[I]) > 0 then
begin
inicio := pos('">', ArquivoXML[I]) + 2;
fim := pos('</deducao', ArquivoXML[I]);
VPis := copy(ArquivoXML[I], inicio, fim - inicio);
end;
end;
// Mostrando na tela os valores encontrados e armazenados nas variáveis
ShowMessage('CSLL: ' + VCsll);
ShowMessage('IRRF: ' + VIr);
ShowMessage('COFINS: ' + VCofins);
ShowMessage('PIS: ' + VPis);
finally
// Liberando o StringList da memória
FreeAndNil(ArquivoXML);
end;
end;Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)