Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamically creating TXMLDocument

1,098 views
Skip to first unread message

Steven Paplanus

unread,
Nov 1, 2001, 3:07:45 PM11/1/01
to
I am trying to create an app that creates a TXMLDocument at runtime. It is
initialized with a ADO recordset that was saved in an XML format to a
WideString. It seems to load it right into the XMLDocument, but when I try
to do a XMLDocument2.DocumentElement, it gives me an access violation.
Below is a sample:


procedure TForm1.Button1Click(Sender: TObject);
var
I : IXMLXmlType;
K : IXMLSchemaType;
L,M,N : IXMLNode;
MyA : TXMLAttributeType;
XMLDocumeNt2 : TXMLDocument;
tempStr : TStrings;
q : integer;
myInfo : TDOMVendor;
begin
tempStr := TStringList.Create;
RemoveDuplicates := TStringList.Create;
Memo1.clear;
memo1.Lines.LoadFromFile('C:\testrds.xml');
tempStr.AddStrings(memo1.Lines);
XMLDocument2 := TXMLDocument.Create('');
xmldocument1.active := false;
XMLDocument1.FileName := '';
XMLDocument2.XML := (tempStr);
xmlDocument2.Active := true;
memo1.Clear;
memo1.Lines.addStrings(XMLDocumeNt2.XML); <-prints out xml fine
Treeview1.Items.Clear;
DomToTree (XMLDocument2.DocumentElement, nil); <-access violation

end;

Please not that DOMTOTree just parses the Document and places it into a
treeview. If anyone has an idea, i would appreciate the help.


steve paplanus


Michael Pham

unread,
Nov 1, 2001, 5:10:43 PM11/1/01
to
Your first document, XMLDocument1 isn't active, hence the Access Violation.


"Steven Paplanus" <sapap...@msn.com> wrote in message
news:3be1ab1c_2@dnews...

Steven Paplanus

unread,
Nov 1, 2001, 5:40:18 PM11/1/01
to
It looks like i have to do a GetXML(XMLDocument2) for it to become properly
initialized. I accidently left in reference to the XMLDOCUMENT1 component
that i left on the form, but that doesn't affect the outcome. The GetXML()
function seems to make the problem go away and make the whole thing work.

steve

"Michael Pham" <mph...@mediaone.net> wrote in message
news:3be1c87a$1_2@dnews...

Mark Edington (Borland)

unread,
Nov 1, 2001, 6:10:27 PM11/1/01
to
"Steven Paplanus" <sapap...@msn.com> wrote in message news:3be1ab1c_2@dnews...

> I am trying to create an app that creates a TXMLDocument at runtime. It is


> initialized with a ADO recordset that was saved in an XML format to a
> WideString. It seems to load it right into the XMLDocument, but when I try
> to do a XMLDocument2.DocumentElement, it gives me an access violation.

When dynamically creating a TXMLDocument you should always assign the result to
an IXMLDocument interface. If TXMLDocument is used as a component, then it's
lifetime is managed by it's owner, if it's created with no owner, then it's
lifetime is manged through the IXMLDocument interface.

Mark


Stig Johansen

unread,
Nov 3, 2001, 7:41:11 AM11/3/01
to
Mark Edington (Borland) wrote:

Well, what a lucky man i am. Just the answer to the question, i was about
to ask.

I now have the following construction:
var
XMLDocument: IXMLDocument;
begin
XMLDocument := TXMLDocument.Create(NIL);
....

Can you tell me, how do i 'free' the XMLDocument after use?

--
Med venlig hilsen / Best regards
Stig Johansen
li...@w3data.dk

Mark Edington (Borland)

unread,
Nov 5, 2001, 3:50:59 PM11/5/01
to
> I now have the following construction:
> var
> XMLDocument: IXMLDocument;
> begin
> XMLDocument := TXMLDocument.Create(NIL);
> ....
>
> Can you tell me, how do i 'free' the XMLDocument after use?

Being an interface, it'll be freed automatically. In your example, if it's a
local variable, it'll get freed when the function returns. You can explicitly
free it by saying XMLDocument := nil;.

Mark


Stig Johansen

unread,
Nov 6, 2001, 12:56:03 AM11/6/01
to
Mark Edington (Borland) wrote:

Hi.

Thanks, It is a local variable, but resides in a function in a
multithreaded/pooled/wsdl/soap server app, so i am just 'scared' of memory
leaks.

So i think i will free it explicitly.

rodrigo.er...@gmail.com

unread,
May 15, 2019, 10:37:03 AM5/15/19
to
Hola, es de esta forma

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
XMLDoc :TXMLDocument;
item :IXMLNode;
component :TComponent;


begin
component :=TComponent.Create(self);
XMLDoc:= TXMLDocument.Create(component);
XMLDoc.Active :=true;
XMLDoc.LoadFromXML(memo1.Text);
documento:='';
memo2.Clear;
for i:=0 to XMLDoc.DocumentElement.ChildNodes.Count -1 Do
Begin

......
0 new messages