PHP - AJAX: Making XML document use DOM at PHP



PHP AJAX Step By Step Tutorial - part 1: At this post, we have made application that handle xml data from server. At that post, we create xml document manually. As you know, at PHP, we can create xml document use DOM. Let's do it.

Create a file named "phpdom.php" within www/test/ajax. Enter following code:

<?php
header('Content-type: text/xml');

$dom = new DOMDocument();

$datas = $dom->createElement('datas');
$dom->appendChild($datas);

$id = $dom->createElement('id');
$idText = $dom->createTextNode('1');
$id->appendChild($idText);

$title = $dom->createElement('title');
$titleText = $dom->createTextNode('PHP Undercover');
$title->appendChild($titleText);

$author = $dom->createElement('author');
$authorText = $dom->createTextNode('Wiwit');
$author->appendChild($authorText);

$book = $dom->createElement('book');
$book->appendChild($id);
$book->appendChild($title);
$book->appendChild($author);

$datas->appendChild($book);

$xmlString = $dom->saveXML();
echo $xmlString;

?>

Point your browser to http://localhost/test/ajax/phpdom.php. You will get like:





Tag: AJAX, DOM, XML Category: Post : March 17th 2008 Read: 1,690 Bookmark and Share

blog comments powered by Disqus