線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2316
推到 Plurk!
推到 Facebook!

TXMLDocument.Create觀念問題請教

尚未結案
bigpipi999
一般會員


發表:4
回覆:4
積分:1
註冊:2004-08-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-17 15:49:45 IP:220.130.xxx.xxx 未訂閱
小弟要在Runtime時自動建立XML的物件 使用了TXMLDocument元件後在Create()這裡找出了一些問題 若使用Create(Nil)或Create('XML檔案名稱') 則後續要找出Node值皆會出現錯誤 但 若使用Create(Form)即給定一Component後即不會出現錯誤 不知是否為Delphi的BUG?? 另外我想問的是 因為我在這檔案中uses Froms這個類別的 Application.Component[0] //Component[x]的值可任意填 這樣的方法是否同等於uses整個Form畫面裡的元件一樣呢?? 使用上是否有什麼不好的地方? 又這樣的方式跟uses相關pas檔差別在哪??
chris_shieh
高階會員


發表:46
回覆:308
積分:240
註冊:2004-04-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-23 09:51:05 IP:219.68.xxx.xxx 未訂閱
什麼情況下出現什麼問題? 我平常這樣用都是沒有問題 TXMLDocument.Create 不就是這樣使用嗎?    建議貼出出問題的程式碼給大家參考一下 也許問題出在別的地方喔    uses 後面可以接 Application.Components[x] ? 沒聽過這種用法?像這樣語法嗎:  
var
  Form1: TForm1;    implementation
  
  uses Aplication.Components[x];
use 後面接的就是Unit name, Unit 不一定是.pas (source) , .dcu 也可 我想你應該是指透過 Application.Components[x] 來取用元件吧 像是這樣嗎?
    procedure TForm2.Button1Click(Sender: TObject);
var
  i:integer;
  form:TForm;
begin
  for  i:= 0 to Application.ComponentCount-1 do
  begin
    if Application.Components[i].Name='Form1' then
    begin
      Showmessage('find form1');
      form:=Application.Components[i] as TForm;
      form.Caption:='I find you in Unit 2';
    end;
  end;
end;
上述form2 裡面 沒有use Form1 因為TApplication 是個全域變數 form2的Button1 Click 之後 還是可以去修改form1 caption 如果你不use 該Unit , 我想很明顯的你不能直接取用元件 每次你都要做元件比對、轉型 當然這樣還是可以存取元件 這樣是可以應用在動態尋找元件 不過如果是大量操作 我想你會很累吧
bigpipi999
一般會員


發表:4
回覆:4
積分:1
註冊:2004-08-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-23 14:28:57 IP:220.130.xxx.xxx 未訂閱
簡單的說我的XML元件是Run Time建的 而且是純一個Util檔沒有掛在任何的元件上 不是一般把元件擺在Form上 所以你上面的程式在Form下當然可以跑 這我試過沒問題,我的問題應該說是在RunTime下 動態建XML文件且Create裡面接的參數會出現 錯誤訊息,故才需要用Application 的方式取得一個元件來附屬 簡單的說一個Unit只能Include Forms元件 則XML建立時用下列方式即可。 XMLDoc:=TXMLDocument.Create(Application.Components[0]); 但若用 XMLDoc:=TXMLDocument.Create(Nil); 或 XMLDoc:=TXMLDocument.Create(FileName); 則到後面在解譯文件時會出現錯誤類似"Access violation..." 這個問題之前有些網友曾提過 目前小弟試出來的結果是有可能 參數為檔名時則在轉型成WideString時該元件會存取錯誤 我現在試的結果是用Application的方式來解決 想問的是這樣一個問題要怎樣轉型才可以套用? 發表人 - bigpipi999 於 2004/12/23 14:54:18
chris_shieh
高階會員


發表:46
回覆:308
積分:240
註冊:2004-04-26

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-24 10:39:19 IP:219.68.xxx.xxx 未訂閱
Delphi Online Help: TXMLDocument: ...When TXMLDocument is created without an Owner, it behaves like an interfaced object. That is, when all references to its interface are released, the TXMLDocument instance is automatically freed. When TXMLDocument is created with an Owner, however, it behaves like any other component, and is freed by its Owner. When you add a TXMLDocument component from the component palette to a form or data module, it is automatically created with an Owner. When the TXMLDocument component is created using the global LoadXMLDocument function (or by a function that the XML Data Binding wizard generates to return the root node of the document), the function creates a TXMLDocument instance without an Owner. 因為沒有Owner的TXMLDocument當 all references to its interface are released, the TXMLDocument instance is automatically freed. 如果不確定建議還是給個Owner, Owner可以在function/procedure 中傳過去 我想問題應該也不在於轉型 因為他會自己type casting 以下是我試的結果 WithOwner的都不會有Access Violation錯誤 WithOutOwner的則不穩定 時常會有Access Violation錯誤 Unit1
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, xmldoc;    type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    uses XMLInstance;    {$R *.dfm}    procedure TForm1.Button1Click(Sender: TObject);
var
  xmlins:TXMLDocument;
  opendlg:TOpenDialog;
begin
  opendlg:=TOpenDialog.Create(self);
  if opendlg.Execute then
    xmlins:=NewXMLWithOwner(self, opendlg.FileName);
  if xmlins<>nil then
    memo1.Lines:=xmlins.XML;
  opendlg.Free;
  xmlins.Free;
end;    procedure TForm1.Button2Click(Sender: TObject);
var
  xmlins:TXMLDocument;
  opendlg:TOpenDialog;
begin
  opendlg:=TOpenDialog.Create(self);
  if opendlg.Execute then
    xmlins:=NewXMLWithOutOwner(opendlg.FileName);
  try
    if Assigned(xmlins) then
    begin
      memo1.Lines:=xmlins.XML;
      xmlins.Free
    end
    else
      showmessage('xmlins already freed!');
  except on E:Exception do
    showmessage('ERROR: '   e.Message);
  end;
  opendlg.Free;
end;    end.    XMLInstance     unit XMLInstance;    interface    uses XMLDoc, Classes;    function NewXMLWithOwner(Owner:TComponent; AFileName:String):TXMLDocument; forward;
function NewXMLWithOutOwner(AFileName:String):TXMLDocument; forward;    implementation    function NewXMLWithOwner(Owner:TComponent; AFileName:String):TXMLDocument;
begin
  Result:=TXMLDocument.Create(Owner);
  Result.LoadFromFile(AFileName);
end;    function NewXMLWithOutOwner(AFileName:String):TXMLDocument;
begin
  Result:=TXMLDocument.Create(AFileName);
end;    end.
發表人 - chris_shieh 於 2004/12/24 11:04:24 發表人 - chris_shieh 於 2004/12/24 11:29:46
pgdennis
資深會員


發表:41
回覆:526
積分:443
註冊:2002-05-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-28 13:49:05 IP:220.140.xxx.xxx 未訂閱
1.如chris_shieh兄所說,當用TXMLDocument.create(nil)時,要格外注意 它基本上是當IXMLDocument在使用,所以不能用Free的,例如 參考http://delphi.about.com/od/adptips2004/a/bltip1004_4.htm 
XMLDoc := TXMLDocument.Create(nil) ;
try
  XMLDoc.Active := True;
  //use XMLDoc here
  XMLDoc.Active := False;
finally
XMLDoc := nil;
end;
這樣當所有refenence都設為nil時,IXMLDocument會自動被釋放掉。 星期一,星期二...星期日..星期一..無窮迴圈@@
------
星期一,二...無窮迴圈@@
系統時間:2024-06-30 11:41:59
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!