Creare file xml

Dvo creare un file XML con i dati che ricevo da un form...

<?php

include 'security.inc.php';

include_once '../common.inc.php';



$doc = new DOMDocument();

$root = $doc->createElement('article');

$root = $doc->appendChild($root);



$timestamp = date('YmdHis');

do {

    $id = 'article' . $timestamp++;

} while (file_exists($fileDir . $id . '.xml'));

$root->setAttribute('id', $id);



$author = $doc->createElement('authorid');

$root->appendChild($author);

$atext = $doc->createTextNode($_POST['authorid']);

$author->appendChild($atext);



$cat = $doc->createElement('categoryid');

$root->appendChild($cat);

$ctext = $doc->createTextNode($_POST['categoryid']);

$cat->appendChild($ctext);



$head = $doc->createElement('headline');

$root->appendChild($head);

$htext = $doc->createTextNode($_POST['headline']);

$head->appendChild($htext);



$desc = $doc->createElement('description');

$root->appendChild($desc);

$dtext = $doc->createTextNode($_POST['description']);

$desc->appendChild($dtext);



$pub = $doc->createElement('pubdate');

$root->appendChild($pub);

$pubtext = $doc->createTextNode(date('Y-m-d'));

$pub->appendChild($pubtext);



$stat = $doc->createElement('status');

$root->appendChild($stat);

$stext = $doc->createTextNode($_POST['status']);

$stat->appendChild($stext);



$key = $doc->createElement('keywords');

$root->appendChild($key);

$ktext = $doc->createTextNode($_POST['keywords']);

$key->appendChild($ktext);



$body = $doc->createElement('body');

$root->appendChild($body);

$cdata = $doc->createCDATASection($_POST['body']);

$body->appendChild($cdata);



$filename = $fileDir . $id . '.xml';

$doc->save($filename);



header('location: articletool.php');

?>

Questo è il file che crea l'articolo ricevendo i dati dal form.

Ho trovato il tutto su un libro, ma da qui non riesco a schiodarmi.

Fino ad ora leggo i file xml, ma quando devo crearne non riesco, mi si blocca a questa pagina.

inviato 9 anni fa
ilbonzo
X 0 X

però non hai spiegato il problema... ricevi dei messaggi di errore?

risposto 9 anni fa
Gianni Tomasicchio
X 0 X

Hai ragione.

ecco l'errore:

Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in /var/www/html/cms/admin/doArticleCreate.php on line 5

Fatal error: Call to undefined method domdocument::createElement() in /var/www/html/cms/admin/doArticleCreate.php on line

risposto 9 anni fa
ilbonzo
X 0 X

Da quello che dice l'errore hai un problema alla riga 5.

Più esattamente qui:

$doc = new DOMDocument();

Dice che manca un parametro nelle parentesi.

 :bye:

risposto 9 anni fa
Marco Dario
X 0 X

Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in /var/www/html/cms/admin/doArticleCreate.php on line 5

Fatal error: Call to undefined method domdocument::createElement() in /var/www/html/cms/admin/doArticleCreate.php on line 6

Alla 5 è un warning, è alla sei che da fatal error.

risposto 9 anni fa
ilbonzo
X 0 X
<?php

include 'security.inc.php';

include_once '../common.inc.php';



$doc = new DOMDocument();

$root = $doc->create_element('article');

$root = $doc->append_child($root);



$timestamp = date('YmdHis');

do {

   $id = 'article' . $timestamp++;

} while (file_exists($fileDir . $id . '.xml'));

$root->setAttribute('id', $id);



$author = $doc->createElement('authorid');

$root->appendChild($author);

$atext = $doc->createTextNode($_POST['authorid']);

$author->appendChild($atext);



$cat = $doc->createElement('categoryid');

$root->appendChild($cat);

$ctext = $doc->createTextNode($_POST['categoryid']);

$cat->appendChild($ctext); 



$head = $doc->createElement('headline');

$root->appendChild($head);

$htext = $doc->createTextNode($_POST['headline']);

$head->appendChild($htext);



$desc = $doc->createElement('description');

$root->appendChild($desc);

$dtext = $doc->createTextNode($_POST['description']);

$desc->appendChild($dtext);



$pub = $doc->createElement('pubdate');

$root->appendChild($pub);

$pubtext = $doc->createTextNode(date('Y-m-d'));

$pub->appendChild($pubtext);



$stat = $doc->createElement('status');

$root->appendChild($stat);

$stext = $doc->createTextNode($_POST['status']);

$stat->appendChild($stext);



$key = $doc->createElement('keywords');

$root->appendChild($key);

$ktext = $doc->createTextNode($_POST['keywords']);

$key->appendChild($ktext);



$body = $doc->createElement('body');

$root->appendChild($body);

$cdata = $doc->createCDATASection($_POST['body']);

$body->appendChild($cdata);



$filename = $fileDir . $id . '.xml';

$doc->save($filename);



header('location: articletool.php');

?>

Cambiato così mi va avanti..

create_element e non createElement.

Ora ho questo errore:

Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in /var/www/html/cms/admin/doArticleCreate.php on line 5

Warning: domnode::append_child() expects parameter 1 to be object, null given in /var/www/html/cms/admin/doArticleCreate.php on line 7

Fatal error: Call to a member function set_attribute() on a non-object in /var/www/html/cms/admin/doArticleCreate.php on line 13

risposto 9 anni fa
ilbonzo
X 0 X
Effettua l'accesso o registrati per rispondere a questa domanda