Recent

Author Topic: Reading an XML file keeps returning and error  (Read 5476 times)

captian jaster

  • Guest
Reading an XML file keeps returning and error
« on: May 11, 2010, 07:26:22 pm »
Code: [Select]
procedure TForm1.OpenBtnClick(Sender: TObject);
var
 TopicNode,ForNode,NoteNode:TDomNode;
begin
  IF(OpenD.Execute)Then
  begin
    Form1.TopicEdit.Clear;
    Form1.ForEdit.Clear;
    Form1.NoteBox.Clear;
    ReadXMLFile(Doc,OpenD.FileName);
    TopicNode := Doc.DocumentElement.FindNode('Topic');
    Form1.TopicEdit.Text := TopicNode.TextContent;
    ForNode := Doc.DocumentElement.FindNode('For');
    Form1.ForEdit.Text := ForNode.TextContent;
    NoteNode := Doc.DocumentElement.FindNode('Note');
    Form1.NoteBox.Append(NoteNode.TextContent);
    Doc.Free;
  end;
end;
This returns an external error called sigve or something like that.. whats wrong? i know i wrote it right...

eny

  • Hero Member
  • *****
  • Posts: 1631
Re: Reading an XML file keeps returning and error
« Reply #1 on: May 11, 2010, 07:31:13 pm »
... i know i wrote it right...
Unfortunately you didn't.
You still don't test for nil values.
Please read the comments given in your previous post.

What's the XML you are trying to read and that gives the error?
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

captian jaster

  • Guest
Re: Reading an XML file keeps returning and error
« Reply #2 on: May 11, 2010, 07:37:18 pm »
if any of the attributes are nil i will get an error.
The XML file im reading from was created by my the same program this is from.
The XML file:
Code: [Select]
<?xml version="1.0"?>
<Notesfile>
  <Info>
    <Topic>Notes. Project</Topic>
    <For>Adam N.Andujar</For>
    <Note>Finish the Open Procedure.</Note>
  </Info>
</Notesfile>

the Error:
External: SigSegV

eny

  • Hero Member
  • *****
  • Posts: 1631
Re: Reading an XML file keeps returning and error
« Reply #3 on: May 11, 2010, 08:02:50 pm »
if any of the attributes are nil i will get an error.
Which makes sense, because you have to test for the nil value!

Try something like this:
Code: [Select]
 Root := Doc.FindNode('Notesfile');
  if assigned(Root) then
  begin
    InfoNode := Root.FindNode('Info');
    if assigned(InfoNode) then
    begin
      // Let's assume all nodes exist, so I'll skip the nil-testing...
      TopicNode := InfoNode.FindNode('Topic');
      ForNode   := InfoNode.FindNode('For');
      NoteNode  := InfoNode.FindNode('Note');
      Form1.TopicEdit.Text := TopicNode.TextContent;
      Form1.ForEdit.Text := ForNode.TextContent;
      Form1.NoteBox.Append(NoteNode.TextContent);
    end;
  end;

Better yet, make a function (or wrapper class.....) that does the searching:
Code: [Select]
  function FindNodeText(const pRoot: TDOMNode; const pNodeName: string): string;
  var node: TDOMNOde;
  begin
    node := pRoot.FindNode(pNodeName);
    if assigned(node) then
      result := node.TextContent
    else
      result := ''
  end;             

  ....
  Root := Doc.FindNode('Notesfile');
  if assigned(Root) then
  begin
    InfoNode := Root.FindNode('Info');
    if assigned(InfoNode) then
    begin
      Form1.TopicEdit.Text := FindNodeText(InfoNode,'Topic');
      Form1.ForEdit.Text   := FindNodeText(InfoNode,'For');
      Form1.NoteBox.Append(FindNodeText(InfoNOde,'Note'));
    end;
  end;         
« Last Edit: May 11, 2010, 08:04:55 pm by eny »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

captian jaster

  • Guest
Re: Reading an XML file keeps returning and error
« Reply #4 on: May 11, 2010, 08:27:00 pm »
Ok thnx eny. im gonna do like you said and check if there nill with a procedure :)

 

TinyPortal © 2005-2018