php dom增加xml节点函数

  1. function: addcomment
  2. adds a comment.
  3. parameters:
  4. $data - associative array of data, must contain 'name', 'website', 'comment', 'date', 'user_ip', 'user_agent', and 'spam'.
  5. returns:
  6. id of the new comment.
  7. <?xml version="1.0" encoding="utf-8"?>
  8. <!doctype messages [
  9. <!element messages (message)*>
  10. <!element message (name , website? , comment , date , user_ip? , user_agent? , spam)>
  11. ]>
  12. <messages>
  13. </messages>
  14. */
  15. public function addcomment($data) {
  16. $xml = new simplexmlelement($this->getcontents(true));
  17. $message = $xml->addchild('message');
  18. $id = $this->generateid();
  19. $message->addattribute('mid', $id);
  20. foreach ($data as $key => $value) {
  21. $message->addchild($key, htmlspecialchars($value, ent_quotes));
  22. }
  23. $this->putcontents($xml->asxml());
  24. return $id;
  25. }//开源代码phpfensi.com