Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $doc = new_xmldoc("1.0");
- //$root = $doc->add_root("employees");
- $xml_file = "finalXML.xml";
- $server = "localhost";
- $username = "root";
- $password = "root";
- $database = "xmlFinal";
- $l = mysql_connect ($server , $username , $password);
- $db = mysql_select_db($database);
- if(!$l){
- echo "Error occured.";
- exit();
- }
- $query = "SELECT * FROM employees";
- $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
- // iterate through resultset
- while($row = mysql_fetch_array($result))
- {
- $record = $root->new_child("employee", "");
- $record->set_attribute("status", $row->status);
- $record->new_child("name", $row->name);
- $record->new_child("position", $row->position);
- $record->new_child("phone", $row->phone);
- $record->new_child("years", $row->years);
- $record->new_child("photo", $row->photo);
- }
- // create doctype
- if (file_exists($xml_file)) {
- // parse document
- $dom = xmldocfile($xml_file);
- // get the root node
- $root = $dom->root();
- // get its children
- $children = $root->children();
- // create child element
- $employee = $dom->create_Element("employee");
- $employee = $root->append_Child($employee);
- // create attribute node
- $employeeatt = $dom->create_Attribute("status",$row->status);
- $employeeatt = $employee->append_Child($employeeatt);
- // create child element
- $firstname = $dom->create_Element("name");
- $firstname = $employee->append_Child($firstname);
- $lastname = $dom->create_Element("position");
- $lastname = $employee->append_Child($lastname);
- // Create all text nodes below
- $text = $dom->create_Text_Node($row->cfn);
- $text = $firstname->append_Child($text);
- $text = $dom->create_Text_Node($row->cln);
- $text = $lastname->append_Child($text);
- if (is_writable($xml_file)){
- $filename = $xml_file;
- $fp = fopen($filename,"w+");
- fputs($fp,$dom->dumpmem());
- fclose($fp);
- echo "<h1>XML file ".$xml_file." has been Updated</h1>";
- echo "<h3>Click <a href='db_to_employees.php'>here</a> to send data to your database.</h3>";
- }else{
- echo $doc->dumpmem();
- echo "<h1>XML file has been updated with new data.</h1>";
- echo "<h3>Click <a href='db_to_employees.php'>here</a> to send data to your database.</h3>";
- }
- }
- ?>
Add Comment
Please, Sign In to add comment