How can I Add newer Data to the start of the xml file (Example inside)

Posted by Ofear on Stack Overflow See other posts from Stack Overflow or by Ofear
Published on 2011-01-08T23:34:31Z Indexed on 2011/01/08 23:53 UTC
Read the original article Hit count: 232

Filed under:
|

Hi All! After alot of work i finish to make a xml reader and writer... but my problem is that i want the newer data that i insert using my form will be added to the top of the xml file. and not in the bottom.. let me show you an example: this is the XML file:

<?xml version="1.0" encoding="utf-8"?>
<events>

<record>
<event>old event</event>
<eventDate>1/1/2009</eventDate>
<desc>old desc</desc>
</record>

<record>
<event>newer event</event>
<eventDate>12/12/2011</eventDate>
<desc>newer desc</desc>
</record>

</events>

now.. I want the XML file to be like this: newer data and then old data

<?xml version="1.0" encoding="utf-8"?>
<events>

<record>
<event>newer event</event>
<eventDate>12/12/2011</eventDate>
<desc>newer desc</desc>
</record>

<record>
<event>old event</event>
<eventDate>1/1/2009</eventDate>
<desc>old desc</desc>
</record>

</events>

This is my add.php (the file that get the POST information from the Form in the main.html :

<?php
$record = array(
 'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);

$doc = new DOMDocument();
$doc->load( 'events.xml' );

$doc->formatOutput = true;
$r = $doc->getElementsByTagName("events")->item(0);

$b = $doc->createElement("record");

$event = $doc->createElement("event");
$event->appendChild(
$doc->createTextNode( $record["event"] )
);
$b->appendChild( $event );

$eventDate = $doc->createElement("eventDate");
$eventDate->appendChild(
$doc->createTextNode( $record["eventDate"] )
);
$b->appendChild( $eventDate );

$desc = $doc->createElement("desc");
$desc->appendChild(
$doc->createTextNode( $record["desc"] )
);

$b->appendChild( $desc );
$r->appendChild( $b );

$doc->save("events.xml");

header("Location: {$_SERVER['HTTP_REFERER']}");    
?>

Where do i need to edit to be able to make the *xml as i want?*

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml