Tuesday, May 12, 2009

MEMBUAT APPEND XML DAN DELETE DENGAN PHP

Membuat Append xml

1. Buatlah file xml sebagai berikut;

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>


2. buatlah file php seperti dibawah ini untuk menciptakan file xml baru...


<?php
$xdoc = new DomDocument;
$xdoc->Load('C:/php/xml_files/candidate.xml');
$candidate = $xdoc->getElementsByTagName('candidate')->item(0);
$newElement = $xdoc ->createElement('candidatedesignation');
$txtNode = $xdoc ->createTextNode ("Project Manager");
$newElement -> appendChild($txtNode);
$candidate -> appendChild($newElement);
$test = $xdoc->save("C:/php/xml_files/candidate2.xml");
echo "<B>Data Appended<B>"
?>


3.Kemudian jalankan file php diatas maka akan tercipta file xml dengan nama candidat2 seperti dibawah ini;

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
<candidatedesignation>Project Manager</candidatedesignation>
</candidate>


MEMBUAT REAKSI DELETE XML

<?php

$xdoc = new DomDocument;
$xdoc->Load('C:/php/xml_files/candidate.xml');
$candidate = $xdoc->getElementsByTagName('candidate')->item(0);
$newElement = $xdoc ->createElement('candidatedesignation');


$txtNode = $xdoc ->createTextNode ("Project Manager");
$txtNode->deleteData(0,8);
$newElement -> appendChild($txtNode);
$candidate -> appendChild($newElement);


$test = $xdoc->save("C:/php/xml_files/candidate2.xml");
echo "<B>Data Appended<B>"


?>

SELAMAT MENCOBA ......