I'm writing a sort of little CMS in PHP. Everything works well, but I've never worked with XML, usually I use SQL DB or CSV. Now I have to append some elements to an XML sheet. I have a function that overwrite completely the file. I'd like to append data.
This is the function "write"
function scrivi() {
if ( isset($_POST["titolo"])) {
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "eventi" );
$doc->appendChild( $r );
$b = $doc->createElement( "evento" );
$titolo = $doc->createElement( "titolo" );
$titolo->appendChild( $doc->createTextNode( $_POST['titolo'] ) );
$b->appendChild( $titolo );
$data = $doc->createElement( "data" );
$data->appendChild( $doc->createTextNode( $_POST['data'] ) );
$b->appendChild( $data );
$desc = $doc->createElement( "desc" );
$desc->appendChild( $doc->createTextNode( $_POST['desc'] ) );
$b->appendChild( $desc );
$r->appendChild( $b );
$doc->save("eventi.xml");
}
}
This is an explicative XML
<?xml version="1.0"?>
<eventi>
<evento>
<titolo>Festa</titolo>
<data>02/03/1993</data>
<desc>Centro Sociale Asilo Politico presenta 99 posse</desc>
</evento>
<evento>
<titolo>Festona</titolo>
<data>15/08/1996</data>
<desc>Falò alla baia: solite facce, solita feccia</desc>
</evento>
</eventi>
I also have a function that, obviously, read data from the XML. I'm able to use the "read" function, from inside the "write" one, so I can read the content, one element at time (with foreach) and insert it in the queue of element to write ($b). It works but I'm not happy of this, I'd like to simply append, or at least read the whole content of the file in a while. I tried copying some snippets here and there but I doesn't find a function I completely understand, and I NEED to understand.
Hope I was specific, Sorry for bad english.
No comments:
Post a Comment