Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create xml file in delphi

I'm new to delphi and now I have to read create an xml. my code is the following:

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.

The problem is that while clicking the save button it shows the exception ,the exception is

Project writexml1.exe raised exception class EAccessViolation  with message 
'Access violation at address 004391B9 in module writexml.exe
like image 257
Abdul Salam Avatar asked Dec 02 '11 10:12

Abdul Salam


2 Answers

your code looks a little complicated. I would suggest: forget TXMLDocument and IDOMDOCUMENT, use IXMLDOCUMENT instead (the way to to use it ist almost the same then TXmlDocument but you dont need a component).

This code should demonstrate, how simple it is:

{...}
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');
{...}

This is how the resulting File would look like:

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

I hope this helps

PS: This sample only needs the Units XMLIntf and XmlDoc so you can tidy your uses a little.

like image 60
knowledge stacker Avatar answered Oct 11 '22 10:10

knowledge stacker


You need to create the XMLDoc instance before you can use it:

XMLDoc := TXMLDocument.Create(...);
try
  ... do stuff with XMLDOC
finally
  XMLDoc.Free;
end;
like image 41
CloudyMarble Avatar answered Oct 11 '22 09:10

CloudyMarble