BarCode99
asked on
Parsing a XMLDocument in a DLL - A.V.
Hi all,
I have a DLL that downloads a XML document from a website or a file share and puts it into a TMemoryStream. I then create a TXMLDocument and load the XML from the stream. Like this:
XMLDocument := TXMLDocument.Create(nil);
try
XMLDocument.LoadFromStream (streamXML File);
XMLDocument.Active := true;
Next I need to get one value out of the XML file and carry on doing other stuff, but here comes my problem... When I try to access XMLDocument.DocumentElemen t I get an Access Violation and I can't figure out why. My code continues like this:
FNewApplicationVersion := XMLDocument.DocumentElemen t.ChildNod es.FindNod e('Applica tionVersio n').Text; <------ Throws exception
except
on E: Exception do begin
ShowMessage('Error: ' + E.Message);
end;
end;
The exception I get is "Access Violation at address 00002000. Read of address 00002000"
My variable declarations look like this:
var
streamXMLFile : TMemoryStream;
XMLDocument : TXMLDocument;
FNewApplicationVersion : String;
As I mentioned this is a DLL. The same code works fine in a normal application... Any ideas?
I have done the trick with including ActiveX in my uses clause and calling CoInitialize in the initialization section.
Best regards,
Marius
I have a DLL that downloads a XML document from a website or a file share and puts it into a TMemoryStream. I then create a TXMLDocument and load the XML from the stream. Like this:
XMLDocument := TXMLDocument.Create(nil);
try
XMLDocument.LoadFromStream
XMLDocument.Active := true;
Next I need to get one value out of the XML file and carry on doing other stuff, but here comes my problem... When I try to access XMLDocument.DocumentElemen
FNewApplicationVersion := XMLDocument.DocumentElemen
except
on E: Exception do begin
ShowMessage('Error: ' + E.Message);
end;
end;
The exception I get is "Access Violation at address 00002000. Read of address 00002000"
My variable declarations look like this:
var
streamXMLFile : TMemoryStream;
XMLDocument : TXMLDocument;
FNewApplicationVersion : String;
As I mentioned this is a DLL. The same code works fine in a normal application... Any ideas?
I have done the trick with including ActiveX in my uses clause and calling CoInitialize in the initialization section.
Best regards,
Marius
XML is case-sensitive. Is it 'ApplicationVersion' or 'applicationversion'?
ASKER
The text is copied straight from the XML file ("ApplicationVersion")...
ASKER
Alternatively, are there other components I can use to parse the XML and read the value of a specific node easily?
I know about OpenXML. Does anybody have any experience with it?
Marius
I know about OpenXML. Does anybody have any experience with it?
Marius
ASKER
OK I worked it out, but maybe someone can tell me what I did wrong???
If I change the declaration of XMLDocument from TXMLDocument to IXMLDocument (I not T) and remove the FreeAndNil it all works. The Create statement is still TXMLDocument.Create...
What about memory? Since there is no FreeAndNil, am I leaking like a sieve? Any thoughts?
Marius
If I change the declaration of XMLDocument from TXMLDocument to IXMLDocument (I not T) and remove the FreeAndNil it all works. The Create statement is still TXMLDocument.Create...
What about memory? Since there is no FreeAndNil, am I leaking like a sieve? Any thoughts?
Marius
no, when you use a local variable as interface type, it is automatically released when you no longer work with (at the end of procedure).
ASKER
OK, so no worries then...
Thanks for you input guys.
I'm going to ask for my points back as I solved it myself...
Marius
Thanks for you input guys.
I'm going to ask for my points back as I solved it myself...
Marius
PAQ and refund acceptable by me.
It's ok for me too.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.