Penggunaan TXMLDoc di Delphi

saya mencoba menulis kembali sebuah unit tersendiri sehingga dapat digunakan untuk mengelola TXMLDoc dan dapat digunakan untuk menggantikan file .INI

unit uPenggunaanXml;

interface

uses
  Forms, SysUtils, Windows, XmlIntf, XMLDoc;

type
  TXMLConfig = class
  private
    FModified: Boolean;
    FFileName: string;
    FXMLDoc: TXMLDocument;
    FBackup: Boolean;
    function GetVersion: string;
  public
    constructor Create(const FileName: string); overload;
    constructor Create; overload;
    destructor Destroy; override;
    procedure Save;
    function ReadString(const Section, Key, default: string): string;
    procedure WriteString(const Section, Key, Value: string);
    function ReadInteger(const Section, Key: string; default: Integer): Integer;
    procedure WriteInteger(const Section, Key: string; Value: Integer);
    function ReadBoolean(const Section, Key: string; default: Boolean): Boolean;
    procedure WriteBoolean(const Section, Key: string; Value: Boolean);
    property Backup: Boolean read FBackup write FBackup;
    property Version: string read GetVersion;
  end;

implementation

{ TXMLConfig }

constructor TXMLConfig.Create(const FileName: string);
begin
  inherited Create;
  FBackup         := True;
  FFileName       := FileName;
  FXMLDoc         := TXMLDocument.Create(Application);
  FXMLDoc.Options := [doNodeAutoIndent];
  if FileExists(FFileName) then
    FXMLDoc.LoadFromFile(FFileName)
  else
  begin
    FXMLDoc.Active := True;
    FXMLDoc.AddChild('Configuration');
  end;
end;

constructor TXMLConfig.Create;
begin
  Create(ChangeFileExt(Application.Exename, '_cfg.xml'));
end;

destructor TXMLConfig.Destroy;
begin
  Save;
  FXMLDoc.Destroy;
  inherited;
end;

function TXMLConfig.GetVersion: string;
begin
  Result := '1.00';
end;

function TXMLConfig.ReadBoolean(const Section, Key: string; default: Boolean): Boolean;
begin
  Result := Boolean(ReadInteger(Section, Key, Integer(default)));
end;

function TXMLConfig.ReadInteger(const Section, Key: string; default: Integer): Integer;
begin
  Result := StrToInt(ReadString(Section, Key, IntToStr(default)));
end;

function TXMLConfig.ReadString(const Section, Key, default: string): string;
var
  Node: IXMLNode;
begin
  Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section);
  if Assigned(Node) and Node.HasAttribute(Key) then
    Result := Node.Attributes[Key]
  else
    Result := default;
end;

procedure TXMLConfig.Save;
begin
  if not FModified then
    Exit;
  if FBackup then

    CopyFile(PChar(FFileName), PChar(FFileName + '.bak'), False);
  FXMLDoc.SaveToFile(FFileName);
  FModified := False;
end;

procedure TXMLConfig.WriteBoolean(const Section, Key: string; Value: Boolean);
begin
  WriteInteger(Section, Key, Integer(Value));
end;

procedure TXMLConfig.WriteInteger(const Section, Key: string; Value: Integer);
begin
  WriteString(Section, Key, IntToStr(Value));
end;

procedure TXMLConfig.WriteString(const Section, Key, Value: string);
var
  Node: IXMLNode;
begin
  if ReadString(Section, Key, '') = Value then
    Exit;
  Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section);
  if not Assigned(Node) then
    Node := FXMLDoc.DocumentElement.AddChild(Section);
  Node.Attributes[Key] := Value;
  FModified := True;
end;

end.

Semoga membantu…

Pemrograman XML dengan PHP

Ini tulisan sederhana untuk menambahkan data pada dokumen XML dengan PHP..
Studi kasus yang ada adalah buku tamu..

Pertama, kita buat terlebih dahulu sebuah file XML bernama “bukutamu.xml”, sebagai berikut:

<?xml version=”1.0″ encoding=”UTF-8″?>
<bukutamu>
<message>
<username>anton</username>
<email>anton@ukdw.ac.id</email>
<web>http://lecturer.ukdw.ac.id/anton</web&gt;
<pesan>hallo coba satu</pesan>
<kode>1</kode>
</message>
</bukutamu>

Kemudian untuk menambahkan data buku tamu baru adalah sebagai berikut:

file: bukutamu.html

Silahkan mengisi buku tamu sebagai berikut:<br>
<form name=”form2″ method=”post” action=”bukutamu2.php”>
<table width=”75%” border=”0″>
<tr>
<td width=”29%”>Uername</td>
<td width=”71%”><input name=”username” type=”text” id=”username” value=””></td>
</tr>
<tr>
<td>Email</td>
<td><input name=”email” type=”text” id=”email” value=””></td>
</tr>
<tr>
<td>Web / Situs</td>
<td> <input name=”web” type=”text” id=”web”>
<font color=”#FF0000″>*must use prefix ‘http://&#8217; first</font></td>
</tr>
<tr>
<td>Pesan</td>
<td><textarea name=”pesan” cols=”40″ rows=”5″ id=”pesan”></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type=”submit” name=”Submit” value=”Isi Buku Tamu”></td>
</tr>
</table>
</form>

Kemudian file bukutamu2.php:

if(isset($_REQUEST[“Submit”])){
$doc = new DOMDocument;
$doc->load(‘bukutamu.xml’);
$doc->formatOutput = true;

$bukutamu = $doc->getElementsByTagName(“bukutamu”);
foreach($bukutamu as $val)
$bukutamunode = $val;

$msgnode = $doc->createElement(‘message’,”);
$msgnode = $bukutamunode->appendChild($msgnode);

$usernode = $doc->createElement(‘username’,”);
$usernode = $msgnode->appendChild($usernode);

$uservalue = $doc->createTextNode($_REQUEST[“username”]);
$uservalue = $usernode->appendChild($uservalue);

$emailnode = $doc->createElement(’email’,”);
$emailnode = $msgnode->appendChild($emailnode);

$emailvalue = $doc->createTextNode($_REQUEST[“email”]);
$emailvalue = $emailnode->appendChild($emailvalue);

$webnode = $doc->createElement(‘web’,”);
$webnode = $msgnode->appendChild($webnode);

$webvalue = $doc->createTextNode($_REQUEST[“web”]);
$webvalue = $webnode->appendChild($webvalue);

$pesannode = $doc->createElement(‘pesan’,”);
$pesannode = $msgnode->appendChild($pesannode);

$pesanvalue = $doc->createTextNode($_REQUEST[“pesan”]);
$pesanvalue = $pesannode->appendChild($pesanvalue);

$h = $doc->save(“bukutamu.xml”);

if($h > 0){
echo “<p>Terimakasih atas partisipasi Anda telah mengisi buku tamu di situs kami.<br>”;

}
}