thefTollege
New Member
I've been using the \[code\]SimpleXMLElement\[/code\] for most of my XML handling, so I was wondering if this is possible using it, but I'm open to using different objects as well.Basically, I maintain all my xml and database records together, so when a database record gets deleted, the XML backup file reflects that change as well. I'm writing a class to handle all of these tasks for me, so basically I do something like this:\[code\]class DataHandler {private $db, $xml; function __construct() { $this->db = new DB; $this->xml = new XML; } public function delete($table, $matches) { $this->db->deleteData($table, $matches); $this->xml->deleteData($table, $matches); }}\[/code\]Then in the DB class I do something like this:\[code\]class DB {public function deleteData($table, $matches) { $whereClause = ''; $c = 0; foreach($matches as $f=>$v) { if ( $c != 0 ) $whereClause .= ' AND '; $whereClause .= $f . '=\'' . $v . '\''; $c++; } $q = 'DELETE FROM ' . $table . ' WHERE ' . $whereClause; return $this->dbquery($q);}}\[/code\]However, I'm not sure what I would do for the XML equivalent of this function. Here is what I have so far, but this only lets me search for one attribute's value, when instead I need to be able to search multiple values and base the decision on that.\[code\]class XML { public function deleteData($table, $matches) { $doc = new DOMDOcument; $doc->loadxml(file_get_contents('db-backup.xml'))); $inst = new DOMXpath($xml); foreach($inst->query('//records[entry/fieldName="' . $fieldValue . '"]') as $node) { $node->parentNode->removeChild($node); } echo $doc->savexml(); }}\[/code\]But as I said, I would like it to accept multiple attributes and values. For instance, if I did not have the \[code\]id\[/code\] attribute of a record but I needed to delete it, I would have to specify more than one attribute. To give an example, if I wanted to delete a user I might do something like this:\[code\]// delete user$dh = new DataHandler;$criteria = array('name'=>'dan', 'age'=>'25', 'email'=>'[email protected]');$delete = $dh->delete('users', $criteria);\[/code\]This works fine in the \[code\]DB\[/code\] class, but how would I do it for \[code\]XML\[/code\] class as well? Thanks in advance, I'm a novice when it comes to XML handling.