Guest User

Untitled

a guest
Jul 29th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2. $doc = new_xmldoc("1.0");
  3. //$root = $doc->add_root("employees");
  4. $xml_file = "finalXML.xml";
  5. $server = "localhost";
  6. $username = "root";
  7. $password = "root";
  8. $database = "xmlFinal";
  9.  
  10. $l = mysql_connect ($server , $username , $password);
  11. $db = mysql_select_db($database);
  12.  
  13. if(!$l){
  14. echo "Error occured.";
  15. exit();
  16. }
  17.  
  18. $query = "SELECT * FROM employees";
  19. $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
  20.  
  21. // iterate through resultset
  22. while($row = mysql_fetch_array($result))
  23. {
  24. $record = $root->new_child("employee", "");
  25. $record->set_attribute("status", $row->status);
  26. $record->new_child("name", $row->name);
  27. $record->new_child("position", $row->position);
  28. $record->new_child("phone", $row->phone);
  29. $record->new_child("years", $row->years);
  30. $record->new_child("photo", $row->photo);
  31. }
  32. // create doctype
  33.  
  34. if (file_exists($xml_file)) {
  35.  
  36. // parse document
  37. $dom = xmldocfile($xml_file);
  38.  
  39. // get the root node
  40. $root = $dom->root();
  41.  
  42. // get its children
  43. $children = $root->children();
  44.  
  45. // create child element
  46. $employee = $dom->create_Element("employee");
  47. $employee = $root->append_Child($employee);
  48.  
  49. // create attribute node
  50. $employeeatt = $dom->create_Attribute("status",$row->status);
  51. $employeeatt = $employee->append_Child($employeeatt);
  52.  
  53. // create child element
  54. $firstname = $dom->create_Element("name");
  55. $firstname = $employee->append_Child($firstname);
  56.  
  57. $lastname = $dom->create_Element("position");
  58. $lastname = $employee->append_Child($lastname);
  59.  
  60.  
  61. // Create all text nodes below
  62. $text = $dom->create_Text_Node($row->cfn);
  63. $text = $firstname->append_Child($text);
  64.  
  65. $text = $dom->create_Text_Node($row->cln);
  66. $text = $lastname->append_Child($text);
  67. if (is_writable($xml_file)){
  68. $filename = $xml_file;
  69. $fp = fopen($filename,"w+");
  70. fputs($fp,$dom->dumpmem());
  71. fclose($fp);
  72. echo "<h1>XML file ".$xml_file." has been Updated</h1>";
  73. echo "<h3>Click <a href='db_to_employees.php'>here</a> to send data to your database.</h3>";
  74. }else{
  75. echo $doc->dumpmem();
  76. echo "<h1>XML file has been updated with new data.</h1>";
  77. echo "<h3>Click <a href='db_to_employees.php'>here</a> to send data to your database.</h3>";
  78.  
  79. }
  80. }
  81. ?>
Add Comment
Please, Sign In to add comment