Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a XML? Easy? I think not! 1

Status
Not open for further replies.

Mithgaur

Technical User
Nov 13, 2007
4
NO
Hei there! I am making a CMS site and I am at the point where I want my members to be able to edit existing articles.
I have come up with the existing code you see below, the only problem is that it doesnt seem to work. Actually.. I dosnt do any thing. I dosnt even output any error messages. Any help would be appriciated

$id = $_POST['id'];
$text = $_POST['text'];
$title = $_POST['title'];
$categori = $_POST['categori'];
$description = $_POST['description'];
$user= $_COOKIE["SESSION"];

$dom = new DomDocument;
$dom->load('articles.xml');
$root = $dom->documentElement;
$articleelement = $dom->createElement('article');
$articleelement->appendChild(new DOMElement('title',utf8_encode('$title')));
$articleelement->appendChild(new DOMElement('text',utf8_encode('$text')));
$articleelement->appendChild(new DOMElement('categori', '$categori'));

$xpath = new DOMXPath($dom);
$oldnode =$xpath->query("//article[@id='$id']")->item(0);

$change_history_element = $dom->createElement('edited');
$articleelement->appendChild($change_history_element);
$change_history_element->appendChild(new DOMElement('name', utf8_encode('$user')));
$change_history_element->appendChild(new DOMElement('time', time()));
$change_history_element->appendChild(new DOMElement('description',
utf8_encode('$description')));
$root->replaceChild($articleelement, $oldnode);
$dom->saveXML();

This is supposed to take a existing article, edit it and post the time of editing, autor who made the change and save the changes he defines in a form.

NOTE: If some of the variable names are mispelled its because I just changed them from Noriwegian to English to make them understandable.

Cheers, Simon
 
[tt] $id = $_POST['id'];
$text = $_POST['text'];
$title = $_POST['title'];
$categori = $_POST['categori'];
$description = $_POST['description'];
$user= $_COOKIE["SESSION"];

$dom = new DomDocument;
$dom->load('articles.xml');
$root = $dom->documentElement;
$articleelement = $dom->createElement('article');
[blue]//you better re-install the id
$articleelement->appendChild(new DOMAttr('id',utf8_encode($id)));[/blue]
$articleelement->appendChild(new DOMElement('title',utf8_encode([blue]$title[/blue])));
$articleelement->appendChild(new DOMElement('text',utf8_encode([blue]$text[/blue])));
$articleelement->appendChild(new DOMElement('categori', [blue]$categori[/blue]));

$xpath = new DOMXPath($dom);
$oldnode =$xpath->query("//article[@id='$id']")->item(0);

$change_history_element = $dom->createElement('edited');
$articleelement->appendChild($change_history_element);
$change_history_element->appendChild(new DOMElement('name', utf8_encode([blue]$user[/blue])));
$change_history_element->appendChild(new DOMElement('time', time()));
$change_history_element->appendChild(new DOMElement('description', utf8_encode([blue]$description[/blue])));
$root->replaceChild($articleelement, $oldnode);
$dom->saveXML();
[/tt]
 
Hey! and thanks for the reply. Unfortunatly, it didnt seem to help, and again no error message either:(
 
Have you read careful enough to see all the differences? Fortunately I really do not need confirmation to know what I posted answer the essential problem in your script. The rest you just have to validate the $id, $title contain something rather than nothing.
 
Yes, sir. Double checked it, copy-pasted it and still nothing.
Again there is not change in the article we edited and no error message. Thank you for using your time to help me, I really appreciated it.
 
Whether there is change or not is not matter of the correctness of the script limited in scope. You have to verify $id is something. Not only it is something, there is an article corresponding to it. The you do the same for the rest request variables. Then you have to ascertain the ambiguous purpose of saveXML. To me, it means you make good use of $doc thereafter. That part I cannot guarantee. If you do instead,
[tt] $dom->save("article_out.xml");[/tt]
what do you see then? These kinds of off-the-script debugging really is not something I have to say - you who do php should know.
 
I changed the last sentence to:

$dom->save('article.xml');

And now it works like a charm!

Thank you so much for your help, Tsuji!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top