如何在 Delphi 中创建xml文件

y53ybaqx  于 2023-03-22  发布在  其他
关注(0)|答案(4)|浏览(360)

我是 Delphi 的新手,现在我必须阅读创建XML。我的代码如下:
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.

问题是,当单击保存按钮时,它显示了异常,异常是

Project writexml1.exe raised exception class EAccessViolation  with message 
'Access violation at address 004391B9 in module writexml.exe
8wtpewkr

8wtpewkr1#

你的代码看起来有点复杂。我建议:忘记TXMLDocumentIDOMDOCUMENT,使用IXMLDOCUMENT代替(使用它的方式几乎与TXmlDocument相同,但您不需要组件)。
这段代码应该演示它是多么简单:

{...}
Var
  XML : IXMLDOCUMENT;
  RootNode, CurNode : IXMLNODE;
begin
  XML := NewXMLDocument;
  XML.Encoding := 'utf-8';
  XML.Options := [doNodeAutoIndent]; // looks better in Editor ;)
  RootNode := XML.AddChild('XML');
  CurNode := RootNode.AddChild('Test');
  CurNode := CurNode.AddChild('Test2');
  CurNode.Text := 'Some Test 2 text';
  CurNode.Attributes['NewAttribute'] := 'Some Test2 Attribute Value';
  XMl.SaveToFile('C:\Documents and Settings\a\Desktop\zulfa.xml');
{...}

这就是生成的文件的样子:

<?xml version="1.0" encoding="utf-8"?>
<XML>
  <Test>
    <Test2 NewAttribute="Some Test2 Attribute Value">Some Test 2 text</Test2>
  </Test>
</XML>

希望这能帮上忙
PS:这个示例只需要Units XMLIntfXmlDoc,所以您可以稍微整理一下您的 * 使用 *。

bfrts1fy

bfrts1fy2#

您需要先创建XMLDoc示例,然后才能使用它:

XMLDoc := TXMLDocument.Create(...);
try
  ... do stuff with XMLDOC
finally
  XMLDoc.Free;
end;
6gpjuf90

6gpjuf903#

SaveClick()正在声明一个本地XMLDoc变量,在使用i之前,该变量没有分配一个有效的TXMLDocument对象。您在TForm上有一个单独的XMLDocument1组件,但您没有使用它。请删除XMLDoc并使用XMLDocument1
此外,您还直接访问了底层的DOMDocument,请使用TXMLDocument自己的方法构建XML,不要下拉到DOM层,除非您需要访问特定于供应商的功能(在这种情况下您不需要)。
试试这个:

procedure TForm1.SaveClick(Sender: TObject); 
var 
  iRoot, iNode, iNode2, iChild: IXMLNode; 
begin 
  XMLDocument1.Active := False;
  XMLDocument1.XML.Text := '';    
  XMLDocument1.Active := True;
  iRoot := XMLDocument.AddChild('xml'); 
  iNode := iRoot.AddChild('test'); 
  iNode.AddChild('test2'); 
  iChild := iNode.AddChild('test3'); 
  iChild.Text := 'Simple value'; 
  iNode.AddChild('test4', iNode.ChildNodes.IndexOf(iChild)); 
  iNode2 := iNode.CloneNode(True); 
  iRoot.ChildNodes.Add(iNode2); 
  iNode2.Attributes['color'] := 'red'; 
  XMLDocument1.SaveToFile('C:\Documents and Settings\a\Desktop\zulfa.xml'); 
  XMLDocument1.Active := False;
end;

生成以下XML:

<?xml version="1.0" encoding="utf-8"?>
<xml>
  <test>
    <test2 />     
    <test4 />
    <test3>Simple value</test3>
  </test>
  <test color="red">
    <test2 />     
    <test4 />
    <test3>Simple value</test3>
  </test>
</xml>

也就是说,使用IXMLDocument而不是TXMLDocument,就像@knowledgestacker建议的那样,通常是更好的选择。

mspsb9vt

mspsb9vt4#

我写了一个库来管理和简化XML文件的工作,你可以检查它here
不知道你想创建什么样的结构,但这里有一个例子:

uses
  ..., IXMLData;

procedure TForm1.Button3Click(Sender: TObject);
var
  d : TIXMLDoc;
  n1, n2 : IXMLNode;
begin
  d := TIXMLDoc.Create('XML');  //By default it uses utf-8 encoding but you specify other encoding as well
  n1 := d.addNode('Test');
  n2 := d.addNode(n1,'Test2');  //add node Test2 as child of node Test
  n2.Attributes['NewAttribute'] := 'Some Test2 Attribute Value';
  n2.Text := 'Some Test 2 text';
  d.SaveTofile('C:\Documents and Settings\a\Desktop\zulfa.xml');  
end;

它将生成以下XML:

<XML>
  <Test>
    <Test2 NewAttribute="Some Test2 Attribute Value">Some Test 2 text</Test2>
  </Test>
</XML>

相关问题