Soy nuevo en delphi y ahora tengo que leer crear un xml. mi código es el siguiente:cómo crear un archivo xml en delphi
unit writexml1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc;
type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SaveClick(Sender: TObject);
var
rootName: String;
childNode: String;
attrChild: string;
iXml: IDOMDocument;
iRoot, iNode, iNode2, iChild, iAttribute: IDOMNode;
XMLDoc: TXMLDocument;
begin
XMLDoc.Active := False;
XMLDoc.XML.Text := '';
XMLDoc.Active := True;
XMLDoc.SaveToFile('C:\Documents and Settings\a\Desktop\zulfa.xml');
iXml := XMLDoc.DOMDocument;
iRoot := iXml.appendChild(iXml.createElement('xml'));
iNode := iRoot.appendChild(iXml.createElement('test'));
iNode.appendChild(iXml.createElement('test2'));
iChild := iNode.appendChild(iXml.createElement('test3'));
iChild.appendChild(iXml.createElement('Simple calue'));
iNode.insertBefore(iXml.createElement('test4'), iChild);
iNode2 := iNode.cloneNode(True);
iRoot.appendChild(iNode2);
iAttribute := iXml.createAttribute('color');
iAttribute.nodeValue := 'red';
iNode2.attributes.setNamedItem(iAttribute);
end;
end.
El problema es que mientras hace clic en el botón Guardar que muestra la excepción, la excepción es
Project writexml1.exe raised exception class EAccessViolation with message 'Access violation at address 004391B9 in module writexml.exe
No está relacionado con su pregunta, pero ¿por qué está guardando el contenido de XMLDoc en un archivo antes de tener XML en él? Parece que su código nunca resultará en un archivo con xml. –