Я просматривал часами, и нет простого объяснения или демонстрации того, как вставить новый дочерний элемент в файл XML, а затем сохранить файл XML.
Вот дерево XML .. (очень просто)
< book > <chapter> <title>Everyday Italian</title> <year>2005</year> </chapter> <chapter> <title>Harry Potter</title> <year>2005</year> </chapter> <chapter> <title>XQuery Kick Start</title> <year>2003</year> </chapter> < / book >
… Я бы очень признателен за любую помощь в этом. Еще раз повторю, у меня есть PHP-файл, и его цель – вставить новую «главу» с указанием «title» и «year», а затем сохранить новый файл (в основном перезаписывая файл book.xml)
Внутри php-руководства есть пример, который дает вам всю необходимую информацию: http://php.net/manual/en/domdocument.save.php
Необходимые методы:
,
<?php //create a document $doc=new DOMDocument; //load the file $doc->load('book.xml'); //create chapter-element $chapter=$doc->createElement('chapter'); //create title-element $title=$doc->createElement('title'); //insert text to the title $title->appendChild($doc->createTextNode('new title for a new chapter')); //create year-element $year=$doc->createElement('year'); //insert text to the year $year->appendChild($doc->createTextNode('new year for a new chapter')); //append title and year to the chapter $chapter->appendChild($title); $chapter->appendChild($year); //append the chapter to the root-element $doc->documentElement->appendChild($chapter); //save it into the file $doc->save('book.xml'); ?>
в<?php //create a document $doc=new DOMDocument; //load the file $doc->load('book.xml'); //create chapter-element $chapter=$doc->createElement('chapter'); //create title-element $title=$doc->createElement('title'); //insert text to the title $title->appendChild($doc->createTextNode('new title for a new chapter')); //create year-element $year=$doc->createElement('year'); //insert text to the year $year->appendChild($doc->createTextNode('new year for a new chapter')); //append title and year to the chapter $chapter->appendChild($title); $chapter->appendChild($year); //append the chapter to the root-element $doc->documentElement->appendChild($chapter); //save it into the file $doc->save('book.xml'); ?>
:<?php //create a document $doc=new DOMDocument; //load the file $doc->load('book.xml'); //create chapter-element $chapter=$doc->createElement('chapter'); //create title-element $title=$doc->createElement('title'); //insert text to the title $title->appendChild($doc->createTextNode('new title for a new chapter')); //create year-element $year=$doc->createElement('year'); //insert text to the year $year->appendChild($doc->createTextNode('new year for a new chapter')); //append title and year to the chapter $chapter->appendChild($title); $chapter->appendChild($year); //append the chapter to the root-element $doc->documentElement->appendChild($chapter); //save it into the file $doc->save('book.xml'); ?>
:<?php //create a document $doc=new DOMDocument; //load the file $doc->load('book.xml'); //create chapter-element $chapter=$doc->createElement('chapter'); //create title-element $title=$doc->createElement('title'); //insert text to the title $title->appendChild($doc->createTextNode('new title for a new chapter')); //create year-element $year=$doc->createElement('year'); //insert text to the year $year->appendChild($doc->createTextNode('new year for a new chapter')); //append title and year to the chapter $chapter->appendChild($title); $chapter->appendChild($year); //append the chapter to the root-element $doc->documentElement->appendChild($chapter); //save it into the file $doc->save('book.xml'); ?>
в<?php //create a document $doc=new DOMDocument; //load the file $doc->load('book.xml'); //create chapter-element $chapter=$doc->createElement('chapter'); //create title-element $title=$doc->createElement('title'); //insert text to the title $title->appendChild($doc->createTextNode('new title for a new chapter')); //create year-element $year=$doc->createElement('year'); //insert text to the year $year->appendChild($doc->createTextNode('new year for a new chapter')); //append title and year to the chapter $chapter->appendChild($title); $chapter->appendChild($year); //append the chapter to the root-element $doc->documentElement->appendChild($chapter); //save it into the file $doc->save('book.xml'); ?>