BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error with dynamic TXMLDocument

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi XML
View previous topic :: View next topic  
Author Message
Rael
Guest





PostPosted: Mon Oct 16, 2006 10:17 pm    Post subject: Error with dynamic TXMLDocument Reply with quote



Hi,

Don't know why this code doesn't work with dynamic TXMLDocument, but works
fine with the component. Please help!

code to create xml file:
procedure TForm1.btnSaveClick(Sender: TObject);
var
iNode: IXMLNode;
begin
XMLDocument1.Active := False;
XMLDocument1.XML.Text := '';
XMLDocument1.Active := True;
// root
XMLDocument1.DocumentElement := XMLDocument1.CreateNode ('root');
iNode := XMLDocument1.DocumentElement.AddChild('node');
iNode.SetAttribute('attr1', 'hello');
iNode.SetAttribute('attr2', 'world');

XMLDocument1.SaveToFile('test.xml');
end;

code to load with xmldocument component (Works fine):
procedure TForm1.btnLoadClick(Sender: TObject);
Var
iRoot, iNode: iXMLNode;
Attr1, Attr2: String;
begin
XMLDocument1.LoadFromFile('test.xml');
iRoot := XMLDocument1.DocumentElement;
iNode := iRoot.ChildNodes.Nodes['node'];
Attr1 := iNode.Attributes['attr1'];
Attr2 := iNode.Attributes['attr2'];
ShowMessage(Attr1+' '+ Attr2);
end;

code using dynamic object: (causes av)
procedure TForm1.btnLoad2Click(Sender: TObject);
Var
XMLDoc: TXMLDocument;
iRoot, iNode: iXMLNode;
Attr1, Attr2: String;
begin
XMLDoc := TXMLDocument.Create(nil);
Try
XMLDoc.LoadFromFile('test.xml');
iRoot := XMLDoc.DocumentElement;
iNode := iRoot.ChildNodes.Nodes['node'];
Attr1 := iNode.Attributes['attr1'];
Attr2 := iNode.Attributes['attr2'];
ShowMessage(Attr1+' '+ Attr2);
Finally
XMLDoc.Free;
End;
end;

Thanks alot
Rael
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Oct 16, 2006 11:54 pm    Post subject: Re: Error with dynamic TXMLDocument Reply with quote



"Rael" <rael (AT) nospam (DOT) com> wrote in message
news:4533beb8 (AT) newsgroups (DOT) borland.com...

Quote:
Don't know why this code doesn't work with dynamic TXMLDocument, but works
fine with the component. Please help!

You should not be using TXMLDocument dynamically to begin with. If you need
to use XML dynamically, then use the IXMLDocument interface instead, ie:

procedure TForm1.btnSaveClick(Sender: TObject);
var
iDoc: IXMLDocument;
iNode: IXMLNode;
begin
iDoc := NewXMLDocument;
iNode := iDoc.CreateNode('root');
iDoc.DocumentElement := iNode;
iNode := iNode.AddChild('node');
iNode.SetAttribute('attr1', 'hello');
iNode.SetAttribute('attr2', 'world');
iDoc.SaveToFile('test.xml');
end;

procedure TForm1.btnLoadClick(Sender: TObject);
var
iDoc: IXMLDocument;
iRoot, iNode: IXMLNode;
Attr1, Attr2: String;
begin
iDoc := LoadXMLDocument('test.xml');
iRoot := iDoc.DocumentElement;
iNode := iRoot.ChildNodes.Nodes['node'];
Attr1 := iNode.Attributes['attr1'];
Attr2 := iNode.Attributes['attr2'];
ShowMessage(Attr1+' '+ Attr2);
end;


Gambit
Back to top
Rael
Guest





PostPosted: Tue Oct 17, 2006 12:54 am    Post subject: Re: Error with dynamic TXMLDocument Reply with quote



Thanks! works fine now....

but as a matter of interest (learning...) why not use TXMLDocument?

Rael
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Oct 17, 2006 2:35 am    Post subject: Re: Error with dynamic TXMLDocument Reply with quote

"Rael" <rael (AT) nospam (DOT) com> wrote in message
news:4533e365 (AT) newsgroups (DOT) borland.com...

Quote:
as a matter of interest (learning...) why not use TXMLDocument?

Because it works differently when instantiated dynamically. According to
the documentation:

"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."

In the code you showed earlier, you were creating the TXMLDocument without
an Owner, which makes it use interface semantics, but your code was not set
up to handle the IXMLDocument interface, so my guess is that the
TXMLDocument was being freed before you had a chance to use it.


Gambit
Back to top
Rael
Guest





PostPosted: Tue Oct 17, 2006 5:03 am    Post subject: Re: Error with dynamic TXMLDocument Reply with quote

Aha... i c , I thought only difference is that I must free object if I set
Owner to nil.

Thanks

Rael
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Oct 17, 2006 6:26 am    Post subject: Re: Error with dynamic TXMLDocument Reply with quote

"Rael" <rael (AT) nospam (DOT) com> wrote in message
news:45341dbd (AT) newsgroups (DOT) borland.com...

Quote:
Aha... i c , I thought only difference is that I must free
object if I set Owner to nil.

That applies to non-interfaced objects. Interfaces are reference-counted
instead. They free themselves automatically when their reference count
falls to 0. Since your code was not using any interface variables, the
count would fall to 0 as soon as Create() exited.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi XML All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.