View Full Version : Help With PHP XML
cmdevuk
07-29-2010, 07:27 PM
hey guys,
i am writing a website for a client and i want to have client testimonials stored as XML, i also want to give people the chance to comment themselves once their work has been completed using a form on the portfolio page.
i need it to just open an existing xml document "pendingtestimonials.xml" and append child nodes to it holding elements named: name, location, comment.
the owner of the business can then look through the testimonials and approve the ones he is happy with, and this will then post the name, location and comment as form elements to a php script that will append them to the "testimonials.xml" that is shown on the portfolio page.
i checked out this http://www.dynamicdrive.com/forums/showthread.php?t=14165 which shows how to read and write to xml in php but it doesnt show how to append to an existing xml document.
any help on this would be appreciated as ive never used php before!
thanks!
look into the simplexml (http://us3.php.net/manual/en/book.simplexml.php) class. It's pretty easy to use. give it a try and ask if you have any questions. :)
cmdevuk
07-30-2010, 07:28 PM
hey there,
thanks for the heads up, altho it doesnt mention anything about saving an XML document, only opening, would i have to open the XML Document each time i wanted to append to it and the re-save it?
hey there,
thanks for the heads up, altho it doesnt mention anything about saving an XML document, only opening, would i have to open the XML Document each time i wanted to append to it and the re-save it?
Yes, you would.
The method SimpleXMLElement::asXML() returns the XML object as an XML string; if you specify a filename (e.g., SimpleXMLElement::asXML('path/to/file.xml')), it will write the XML to the file instead.
cmdevuk
07-31-2010, 06:53 PM
right ok thanks for the help so far, i have looked at the DOMDocument class as that is how it loads the XML Data for the portfolio testimonials at the moment.
i have a script there now to open the xmlfile and append a new child elemen to it then save the new document but im getting this error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/a8186275/public_html/scripts/submitcomment.php on line 6
<?php
$doc = new DOMDocument();
$doc->load( '../pendingtestimonials.xml');
$holder = doc->createElement('testimonial','');
$element1 = $doc->createElement('name', $_POST['name']);
$element2 = $doc->createElement('location', $_POST['location']);
$element3 = $doc->createElement('comment', $_POST['comment']);
$holder->appendChild($element1);
$holder->appendChild($element2);
$holder->appendChild($element3);
$doc->appendChild($holder);
$doc->saveXML();
$doc = new DOMDocument();
$doc->load( '../pendingtestimonials.xml');
echo($doc);
?>
any ideas???
cmdevuk
07-31-2010, 06:54 PM
my bad i forgot the $ haha...
cmdevuk
07-31-2010, 07:17 PM
$doc = new DOMDocument();
$doc->load( 'pendingtestimonials.xml');
$holder = $doc->createElement('testimonial','');
$element1 = $doc->createElement('name', $_POST['name']);
$element2 = $doc->createElement('location', $_POST['location']);
$element3 = $doc->createElement('comment', $_POST['comment']);
$holder->appendChild($element1);
$holder->appendChild($element2);
$holder->appendChild($element3);
$doc->appendChild($holder);
$doc->saveXML();
i am confused now as this script does not add any child elements to the xml file. it is empty still
<?xml version="1.0" encoding="utf-8"?>
<ClientTestimonials>
</ClientTestimonials>
thats what the xml file contains at the moment....
any ideas where i have gone wrong??
cmdevuk
07-31-2010, 07:34 PM
i changed $doc->saveXML(); to $doc->save('pendingtestimonials.xml');
and now its telling me:
Warning: DOMDocument::save(pendingtestimonials.xml) [domdocument.save]: failed to open stream: Permission denied in /home/a8186275/public_html/scripts/submitcomment.php on line 17
well, personally, I don't like DOMDocument; I think SimpleXML is much easier to work with. I'm not sure about your problems forming the xml tree, but the issue with saving it looks like a permissions problem. Make sure your script has write permissions for the /scripts/ directory.
Try
<?php
// your code here
echo $doc->saveXML();
?>and see what it returns; then you'll know if the xml is actually being built or not.
cmdevuk
08-01-2010, 06:54 PM
$doc = new DOMDocument();
$doc->load( 'pendingtestimonials.xml');
$holder = $doc->createElement('testimonial','');
$node = $doc->getElementsByTagName("ClientTestimonials");
$element1 = $doc->createElement('name', $_POST['name']);
$element2 = $doc->createElement('location', $_POST['location']);
$element3 = $doc->createElement('comment', $_POST['comment']);
$holder->appendChild($element1);
$holder->appendChild($element2);
$holder->appendChild($element3);
$node->documentElement->appendChild($holder);
$doc->appendChild($node);
$doc->save('pendingtestimonials.xml');
thats what i have now, and at the point $node i want it to append $holder to root element $node but its telling this error:
Fatal error: Call to a member function appendChild() on a non-object in /home/a8186275/public_html/scripts/submitcomment.php on line 15
sorry to be a pain but i am not familiar with php and i dont really know what on earth im doing lol
cmdevuk
08-01-2010, 07:22 PM
thanks for ur continued support, i am going to take this over to MySQL instead, a database table is far easier to work with :)
A database is a better choice for many applications. There are things an xml tree works better for, however - if you ever try it again, I'd recommend SimpleXML over DOMDocument. Good luck!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.