Im try develop a helper to read/write and control an instance of TXMLDocument. I write a simple unit for this work. The unit have a procedure that assign the instance to global variable, and set some variables for document control. The unit is:
unit Globals;
{ Variables globales de la aplicacion, con sus correspondientes accessors }
interface
uses
{ XML Helper }
xmldom, XMLIntf, msxmldom, XMLDoc, SysUtils, DateUtils;
type
XmlCheckPoint = Record
asociado: boolean;
xmlFile: TXMLDocument;
saved: boolean;
lastModification: TDateTime;
lastSave: TDateTime;
path: TFilename;
End;
{ Firmas }
procedure assignXml(var aXml: TXMLDocument);
procedure xmlWriteProyectoNode(obra,cliente,ubicacion,fecha,sondeo,estudio: String);
function existsXml(): boolean;
function xmlIsUpdated(): boolean;
var
Xml: XmlCheckPoint;
The procedure assignXml, works fine:
procedure assignXml(var aXml: TXMLDocument);
begin
Xml.xmlFile := aXml;
Xml.asociado := true;
Xml.saved := false;
Xml.lastSave := Yesterday;
Xml.path := '';
{ Inserto el nodo raiz }
Xml.xmlFile.Active := true;
Xml.xmlFile.AddChild('raiz');
Xml.lastModification := Now();
end;
But, xmlWriteProyectoNode(...) explodes the app:
procedure xmlWriteProyectoNode(obra,cliente,ubicacion,fecha,sondeo,estudio: String);
var
root,meta,child: IXMLNode;
begin
Xml.xmlFile.Active := true;
root := Xml.xmlFile.DocumentElement;
meta := root.AddChild('proyecto');
child := meta.AddChild('obra');
child.Text := obra;
[...]
Xml.lastModification := Now();
end;
The app crash when invoques writeXmlProyectoNode(...) with an Access Violation Error. In execution time. The Embarcadero debuger says that the conflicting line is:
root := Xml.xmlFile.DocumentElement;
I need get the root element, and was think this was the correct way... Im newest in Delphi, any ideas ?. Thanks !.
Edit:
The XML Creation ( newXml type is TXMLDocument )
newXml := TXMLDocument.Create(nil);
newXml.Options := [doNodeAutoIndent];
newXml.Active := true;
{ Asocio la instancia de XMLDocument a mi variable global newXml}
Globals.assignXml(newXml);
Xmlin parameters ofprocedure xmlWriteProyectoNode. So - where does it take it from ? and what is the value ofXMLat the start of procedure. Add the 1st line ofprocedure xmlWriteProyectoNodeasShowMessage(IntToStr(Integer(Pointer(XML))));TXMLDocument! this class has a number of traps and how you create it is one of those! BTW, "with an Access Violation Error" is better than nothing, but is less than what Delphi told you. Please copy the exact error message here.root := Xml.xmlFile.DocumentElement;is complex line. And "complex" is the same as "ambiguous". Split it to simpler lines.tempvar := Xml.xmlfile; root := tempvar.DocumentElement;