Tôi đang tạo một thể hiện của TXMLDocument khi chạy, để tải và phân tích cú pháp tệp XML. Bạn có thể kiểm tra mã bên dưới:Delphi - TXMLTài liệu được tạo tại thời gian chạy tạo AV, với thành phần trên biểu mẫu đang hoạt động
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
type
Txml = class(TForm)
// XMLDocument1: TXMLDocument;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
xml: Txml;
implementation
{$R *.dfm}
procedure Txml.FormCreate(Sender: TObject);
var i,j:integer;
aNode:IXMLNode;
ws:String;
XMLDocument1:TXMLDocument;
begin
Memo1.Lines.Clear;
XMLDocument1 := TXMLDocument.Create(nil);
try
XMLDocument1.LoadFromFile('C:\a.xml');
XMLDocument1.Active := true;
aNode := XMLDocument1.ChildNodes.First;
while aNode<>nil do
begin
for i := 0 to aNode.ChildNodes.Count-1 do
begin
if aNode.ChildNodes[i].NodeName = 'Role' then
begin
Memo1.Lines.Add('Tag - '+aNode.ChildNodes[i].ChildNodes['Tag'].Text);
for j := 0 to aNode.ChildNodes[i].ChildNodes.Count-1 do
if aNode.ChildNodes[i].ChildNodes[j].HasChildNodes then
begin
ws := VarToStr(aNode.ChildNodes[i].ChildNodes[j].ChildValues['Tag']);
if trim(ws)<>'' then
Memo1.Lines.Add(ws);
ws := VarToStr(aNode.ChildNodes[i].ChildNodes[j].ChildValues['Value']);
if trim(ws)<>'' then
Memo1.Lines.Add(ws);
end;
end;
end;
aNode := aNode.NextSibling;
end;
XMLDocument1.Active := false;
finally
FreeAndNil(XMLDocument1);
end;
end;
end.
Vấn đề là tạo ra AV. Như bạn có thể đã thấy, trước khi thành phần có trên biểu mẫu (// XMLDocument1: TXMLDocument;).
Tại sao khi thành phần ở trên biểu mẫu mã đang hoạt động, nhưng khi tôi tạo mã trong thời gian chạy, nó tạo AV?
LE: giải pháp: dựa trên các câu trả lời/bình luận và Delphi Trợ giúp:
XMLDocument1 : IXMLDocument; //not TXMLDocument
XMLDocument1 := LoadXMLDocument(...);
FreeAndNil;// must be deleted
Sử dụng [IXMLDocument] (http://docwiki.embarcadero.com/VCL/en/XMLIntf.IXMLDocument) và như constructor [NewXMLDocument] (http://docwiki.embarcadero.com/VCL/en/ XMLDoc.NewXMLDocument). – TLama
AV xảy ra ở đâu? – Kromster
Bạn nên làm 'ANode: = nil;' sau vòng lặp để giải phóng thể hiện nút trước khi đóng và giải phóng 'XMLDocument1'. –