Forum > General

Reading an XML file keeps returning and error

(1/1)

captian jaster:

--- Code: ---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;

--- End code ---
This returns an external error called sigve or something like that.. whats wrong? i know i wrote it right...

eny:

--- Quote from: captian jaster on May 11, 2010, 07:26:22 pm ---... i know i wrote it right...

--- End quote ---
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?

captian jaster:
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: ---<?xml version="1.0"?>
<Notesfile>
  <Info>
    <Topic>Notes. Project</Topic>
    <For>Adam N.Andujar</For>
    <Note>Finish the Open Procedure.</Note>
  </Info>
</Notesfile>

--- End code ---

the Error:
External: SigSegV

eny:

--- Quote from: captian jaster on May 11, 2010, 07:37:18 pm ---if any of the attributes are nil i will get an error.
--- End quote ---
Which makes sense, because you have to test for the nil value!

Try something like this:

--- Code: ---  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;

--- End code ---

Better yet, make a function (or wrapper class.....) that does the searching:

--- Code: ---  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;         

--- End code ---

captian jaster:
Ok thnx eny. im gonna do like you said and check if there nill with a procedure :)

Navigation

[0] Message Index

Go to full version