devdavad Posted December 15, 2008 Share Posted December 15, 2008 Hi, I'm working on a simple web blog and have come to a point where I need to ask for some help. So I'm working with this xml file <?xml version="1.0" encoding="utf-8"?> <blogging> <entry title="another post"> <date>Sunday the 14 of December, 2008 at 08:42 AM</date> <blog>A different post than the first one</blog> </entry> <entry title="first post"> <date>Saturday the 13 of December, 2008 at 08:47 PM</date> <blog>the first post</blog> </entry> </blogging> and I want to insert another entry along with it's respective children (date, and blog). And here's the php code I've come up with so far <?php # %A the %d of %B, %Y at %I:%M %p echo "<html><head>\n<title>"; if(isset($_POST["entry"], $_POST["blog"]) == True) { echo "new post entered</title>\n</head>"; # initialize DOM stuff $dom = new DOMDocument("1.0", "utf-8"); $dom->load("blogging.xml"); $dom->preserveWhiteSpace = true; $xpath = new DOMXPath($dom); $lastpost = $xpath->query("/blogging/entry[1]")->item(0); $entry = $dom->createElement("entry"); $entry->setAttribute("title", $_POST["entry"]); $date = $dom->createElement("date", strftime("%A the %d of %B, %Y at %I:%M %p")); $blog = $dom->createElement("blog", $_POST["blog"]); $newpost = $dom->appendChild($entry); # need to also append $date and $blog so that they are children of $entry but I'm not for sure how # put the stuff that we are going to append right before the latest post $lastpost->parentNode->insertBefore($newpost, $lastpost); #file_put_contents("blogging.xml", $dom->saveXML()); echo $dom->saveXML(); } else { # display the form for writing a new blog post echo "editting webblog @ " . $_SERVER["SERVER_NAME"] . "</title>\n</head>"; echo "<body>\n" . "<h1 class=\"title\">Edit the webblog below</h1>\n" . "<p></p>\n<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"post\">\n" . "Blog post title: <input type=\"text\" name=\"entry\" />\n" . "<p></p>\nEnter blog: <textarea rows=\"10\" cols=\"50\" name=\"blog\"></textarea>\n" . "<p></p>\n<button type=\"submit\">submit</button>\n</form>\n"; } # end the html stuff echo "</body>\n</html>"; ?> Link to comment https://forums.phpfreaks.com/topic/137006-solved-trying-to-insert-nested-xml-data-with-dom/ Share on other sites More sharing options...
devdavad Posted December 15, 2008 Author Share Posted December 15, 2008 this is how you do it, noob: <?php echo "<html><head>\n<title>"; if(isset($_POST["entry"], $_POST["blog"]) == True) { echo "new post entered</title>\n</head>"; # initialize DOM stuff $dom = new DOMDocument("1.0", "utf-8"); $dom->load("blogging.xml"); $dom->preserveWhiteSpace = true; $xpath = new DOMXPath($dom); $lastpost = $xpath->query("/blogging/entry[1]")->item(0); $entry = $dom->createElement("entry"); $entry->setAttribute("title", $_POST["entry"]); $date = $dom->createElement("date"); $thedate = $dom->createTextNode(strftime("%A the %d of %B, %Y at %I:%M %p")); $date->appendChild($thedate); $blog = $dom->createElement("blog"); $theblog = $dom->createTextNode($_POST["blog"]); $blog->appendChild($theblog); $entry->appendChild($date); $entry->appendChild($blog); $newpost = $dom->documentElement->appendChild($entry); # put the stuff that we are going to append right before the latest post $lastpost->parentNode->insertBefore($newpost, $lastpost); #file_put_contents("blogging.xml", $dom->saveXML()); echo $dom->saveXML(); } else { # display the form for writing a new blog post echo "editting webblog @ " . $_SERVER["SERVER_NAME"] . "</title>\n</head>"; echo "<body>\n" . "<h1 class=\"title\">Edit the webblog below</h1>\n" . "<p></p>\n<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"post\">\n" . "Blog post title: <input type=\"text\" name=\"entry\" />\n" . "<p></p>\nEnter blog: <textarea rows=\"10\" cols=\"50\" name=\"blog\"></textarea>\n" . "<p></p>\n<button type=\"submit\">submit</button>\n</form>\n"; } # end the html stuff echo "</body>\n</html>"; ?> thread solved Link to comment https://forums.phpfreaks.com/topic/137006-solved-trying-to-insert-nested-xml-data-with-dom/#findComment-716030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.