Como ler XML de retorno

Delphi

18/11/2008

Como posso criar uma rotina qu eu possa ler as TAGS <MSG>
deste arquivo XML de retorno presciso somente do conteudo da MSG ( MSG_ID, MSG_MISN, MSG_STATUS, MSG_STATUS_DATETIME )


Estou utilizando D7,
Segue abaixo a estrutura do XML


<DISPARA>
<GROUP_MSG>
<MSG>
<MSG_ID>123456789</MSG_ID>
<MSG_MISN>1978191332</MSG_MISN>
<MSG_STATUS>1002</MSG_STATUS>
<MSG_STATUS_DATETIME>2007-10-17 1:57:12</MSG_STATUS_DATETIME>
</MSG>

<MSG>
<MSG_ID>123456759</MSG_ID>
<MSG_MISN>1978193332</MSG_MISN>
<MSG_STATUS>1003</MSG_STATUS>
<MSG_STATUS_DATETIME>2007-10-17 1:57:12</MSG_STATUS_DATETIME>
</MSG>

</GROUP_MSG>
</DISPARA>


Walter Faria

Walter Faria

Curtidas 0

Respostas

Walter Faria

Walter Faria

18/11/2008

Eai, gente niguem pode me ajudar a fazer essa leitura do XML?


GOSTEI 0
Tonidavi2004

Tonidavi2004

18/11/2008

[b:69d086815a]Walter Faria[/b:69d086815a], vc pode utilizar este script:
var
  xml: TextFile;
  Str: string;
  StrInicial: string;
  StrFinal: string;
begin
  AssignFile(xml, ´C:\Documents and Settings\tdavi\Desktop\Teste.xml´);
  Reset(xml);
  try
    while not Eof(xml) do
    begin
      Readln(xml, Str);
      if (Str = ´<MSG>´) then
      begin
        ClientDataSet1.Append;
        Readln(xml, Str);

        StrInicial := Copy(Str, 1, Pos(´<MSG_ID>´, Str)+ 7);
        StrFinal   := Copy(Str, Pos(´</MSG_ID>´, Str), Length(Str));

        if (StrInicial = ´<MSG_ID>´) then
          ClientDataSet1.Fields.Fields[0].AsString := Copy(Str, Length(StrInicial)+ 1, Length(Str)- (Length(StrInicial)+ Length(StrFinal)));

        Readln(xml, Str);
        StrInicial := Copy(Str, 1, Pos(´<MSG_MISN>´, Str)+ 9);
        StrFinal   := Copy(Str, Pos(´</MSG_MISN>´, Str), Length(Str));

        if (StrInicial = ´<MSG_MISN>´) then
          ClientDataSet1.Fields.Fields[1].AsString := Copy(Str, Length(StrInicial)+ 1, Length(Str)- (Length(StrInicial)+ Length(StrFinal)));

        Readln(xml, Str);
        StrInicial := Copy(Str, 1, Pos(´<MSG_STATUS>´, Str)+ 11);
        StrFinal   := Copy(Str, Pos(´</MSG_STATUS>´, Str), Length(Str));

        if (StrInicial = ´<MSG_STATUS>´) then
          ClientDataSet1.Fields.Fields[2].AsString := Copy(Str, Length(StrInicial)+ 1, Length(Str)- (Length(StrInicial)+ Length(StrFinal)));

        Readln(xml, Str);
        StrInicial := Copy(Str, 1, Pos(´<MSG_STATUS_DATETIME>´, Str)+ 20);
        StrFinal   := Copy(Str, Pos(´</MSG_STATUS_DATETIME>´, Str), Length(Str));

        if (StrInicial = ´<MSG_STATUS_DATETIME>´) then
          ClientDataSet1.Fields.Fields[3].AsString := Copy(Str, Length(StrInicial)+ 1, Length(Str)- (Length(StrInicial)+ Length(StrFinal)));

        Readln(xml, Str);
        if (Str = ´</MSG>´) then
          ClientDataSet1.Post;
      end;
    end;
  finally
    CloseFile(xml);
  end;
end;

Importante dizer que a estrutura do xml não pode ser mudada.


GOSTEI 0
POSTAR