Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help With PHP XML

  1. #1
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With PHP XML

    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/s...ad.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!

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    look into the simplexml class. It's pretty easy to use. give it a try and ask if you have any questions.

  3. #3
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default RE: SimpleXML Class

    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?

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by cmdevuk View Post
    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.

  5. #5
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 Code:
    <?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???

  6. #6
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    my bad i forgot the $ haha...

  7. #7
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    $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??

  8. #8
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 Code:
    <?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.

  10. #10
    Join Date
    Jul 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    $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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •