Hi,
I must have posted answers to this type or related question in the past. Here we go again take Four.. I want to do this my way, or we can just take the highway..
First, we create our new books.xml file
<?xml version="1.0" standalone="yes"?>
<books>
<book>
<author>Poor Boy or veedeoo</author>
<title>Hacking is Bad</title>
<publisher>the black book</publisher>
</book>
<book>
<author>veedeoo</author>
<title>The Secret of Hacking for Revenge</title>
<publisher>the black book</publisher>
</book>
</books>
Second, we create the php script responsible for writing and displaying the changes made on the books.xml file. Let's call this as book.php .
<?php
## filename : book.php
$xml = "books.xml";
$doc = new DomDocument;
$doc->Load($xml);
$books = $doc->getElementsByTagName('books')->item(0);
## we iterate book block
$new_BookEntry = $doc ->createElement('book');
## items going inside the book block
$new_AuthorEntry = $doc->createElement('author');
$new_TitleEntry = $doc ->createElement('title');
$new_PubEntry = $doc->createElement('publisher');
## prepare items to be added in xml file
$authorNode = $doc ->createTextNode ('New Book');
$titleNode = $doc ->createTextNode ('New Title');
$pubNode = $doc->createTextNode ('new publisher');
$new_AuthorEntry-> appendChild($authorNode);
$new_TitleEntry-> appendChild($titleNode);
$new_PubEntry->appendChild($pubNode);
$new_BookEntry-> appendChild($new_AuthorEntry);
$new_BookEntry-> appendChild($new_TitleEntry);
$new_BookEntry->appendChild($new_PubEntry);
$books -> appendChild($new_BookEntry);
if($doc->save($xml)) echo "Sucess";
## read updated xml file
//$doc = new DOMDocument();
//$doc->load( $xml );
$records = $doc->getElementsByTagName( "book" );
foreach( $records as $record )
{
$author = $record->getElementsByTagName( "author" );
$author = $author->item(0)->nodeValue;
$publishers = $record->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $record->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "<p>Title: ".$title."<br/>Author: ".$author."<br/>Publisher: ".$publisher."<br/></p>";
}
?>
That's should do it..direct your browser to book.php and you should be able to read the newly added items.
Room for imporvements, the script above, should be so easy to modify for any purpose e.g. form based application.., or even database application.
Looking at the script above, it is also pretty easy to make a simple function out of it, or even a simple class to make a multiple entries on the xml file, without actually editing the book.php .