【发布时间】:2011-12-02 10:06:01
【问题描述】:
我是 delphi 新手,现在我必须阅读创建 xml。我的代码如下:
单元写入xml1;
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.
问题是点击保存按钮时显示异常,异常是
Project writexml1.exe raised exception class EAccessViolation with message 'Access violation at address 004391B9 in module writexml.exe
【问题讨论】:
-
与您的问题无关,但是为什么要在包含任何 XML 之前将 XMLDoc 的内容保存到文件中?看起来您的代码永远不会生成包含 xml 的文件。