views:

134

answers:

1

Hi

I am parsing an XML file and storing the results in a record, but am having some trouble.

Im trying to store the results (content of my XML tags) into the fields of my record..

My record (at the moment there is only 1 set of XML elements). I think that the Parser.curconten is causing the problem...

    Type

TXMLAlert=Record
alert, desc, action:string;
end;

Var
MyXMLAlert:TXMLAlert;

MyXMLAlert.alert:=Parser.CurContent
MyXMLAlert.desc:=Parser.CurContent
MyXMLAlert.action:=Parser.CurContent

The following is my parser code;

procedure ProcessXML();
var
  Parser : TXmlParser;
  rule, alert: string;
  i:integer;
  memo1:Tmemo;


begin
  Parser := TXmlParser.Create;
  Parser.Normalize := TRUE;
  Parser.LoadFromFile ('c:\parser.xml');
  Parser.StartScan;

  while Parser.Scan do
    case Parser.CurPartType of
     ptStartTag,
    ptEmptyTag : Form1.Memo1.Lines.Add ('New Element: ' + Parser.CurName);
    ptContent  : Form1.Memo1.Lines.Add ('Content of Element ' +
    Parser.Curname + ':' + Parser.CurContent);
      end;
  Parser.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  ProcessXML();
end;

end.

Program parser fine and the content of tags is displayed in memo1... Any ideas why the record is not picking up the results of content? Thanks, Lazerspewpew

+1  A: 

It doesn't look like you're calling Scan between each call to CurContent in your record code, so you won't actually advance through the input. It also doesn't look like your record code is skipping over start and end tags the way you memo code is. For that matter, I can't be sure you're doing any of the same setup in your record code as you are in your memo code. At the point where your record code runs, does Parser refer to a valid TXmlParser instance that has loaded data from a file yet? Has it started scanning yet?

The haphazard indentation and the several unused variables makes it hard to be confident we're really seeing code that causes the problem you describe anyway.

What does the memo control get? (Copy and paste.) And what does the record get instead of what you expected?

Rob Kennedy