cobusbo Posted March 1, 2013 Share Posted March 1, 2013 Hi I have this document <?xml version="1.0" encoding="UTF-8"?> <users> <user><name>Skuilnaam</name><score>8</score></user><user><name>Skuilnaam1</name><score>10</score></user><user><name>cobus</name><score>5</score></user></users> I need help to make a php script that will delete the records inside this page for me can anyone help me to construct such a page please? Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/ Share on other sites More sharing options...
teynon Posted March 1, 2013 Share Posted March 1, 2013 What have you tried? - http://www.whathaveyoutried.com Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415750 Share on other sites More sharing options...
cobusbo Posted March 1, 2013 Author Share Posted March 1, 2013 to be honest I have no idea where to start, ive been googling and found examples like these ones <?php $xml = '<root> <featured> <title></title> <tweet></tweet> <img></img> </featured> </root>'; $dom = new DOMDocument(); $dom->loadXML($xml); $featuredde1 = $dom->getElementsByTagName('featured'); foreach ($featuredde1 as $node) { $node->parentNode->removeChild($node); } echo $dom->saveXML(); The output is: <?xml version="1.0"?> <root> </root> but im not sure how to modify it for my needs, im a beginner and still learning the basics Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415776 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 You first order of business would then be to read about DOMdocument. If that still doesn't help, you might want to start with something more basic, to ensure that you are familiar with the PHP syntax before moving onto some of the more advanced topics. The PHP manual usually contains all of the information you need, so it is highly recommended to read through it. Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415785 Share on other sites More sharing options...
cobusbo Posted March 1, 2013 Author Share Posted March 1, 2013 this is what I got so far my leaders.xml document <?xml version="1.0" encoding="UTF-8"?> <users> <user><name>Skuilnaam</name><score>8</score></user><user><name>Skuilnaam1</name><score>10</score></user><user><name>cobus</name><score>5</score></user></users> and here i got my file which i want to run to delete my database entries and cleaning the xml entries but im have problem with my xml deletion part <? $db_host = "xxxxxxxxxxx"; $db_user = "xxxxxxxxxxxx"; $db_pass = "xxxxxxxxxxxxxx"; $db_name = "xxxxxxxxxxx"; $connect = @mysql_connect($db_host,$db_user,$db_pass); @mysql_select_db($db_name); static $leaderboardfile = 'res/leaders.xml'; // Empty table $query = "TRUNCATE TABLE rente"; mysql_query($query); ?> <?php $retriever = new DOMDocument(); $retriever ->load( 'res/leaders.xml' ); $masterElement = $retriever->documentElement; $masterContent = $masterElement->getElementsByTagName('users')->item(3); $oldContent = $masterElement->removeChild($masterContent ); echo $retriever->saveXML(); ?> I get the following error message: Catchable fatal error: Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in /srv/disk1/1296487/www/rekvasvra.uni.me/Gr10/Rente/cron.php on line 23 can you guys please assist me with this error Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415798 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 If you add var_dump ($masterElement->getElementsByTagName('users')); just before the line with the error, what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415805 Share on other sites More sharing options...
cobusbo Posted March 1, 2013 Author Share Posted March 1, 2013 then it gives me this result object(DOMNodeList)#3 (1) { ["length"]=> int(0) }Catchable fatal error: Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in /srv/disk1/1296487/www/rekvasvra.uni.me/Gr10/Rente/cron.php on line 24 If you add var_dump ($masterElement->getElementsByTagName('users')); just before the line with the error, what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415807 Share on other sites More sharing options...
teynon Posted March 1, 2013 Share Posted March 1, 2013 I actually just posted something very similar to this See http://forums.phpfreaks.com/topic/275068-extract-data-from-html-table/?do=findComment&comment=1415740 I added a function "getElementsByTagName" so you can extract data easier. Here is how you might do it: <?phprequire_once("DOMe.php");$dom = new DOMe("div");$dom->importHTML(file_get_contents("file.html"));echo $dom->generate();$rows = $dom->getElementsByTagName("tr");$data = array();foreach ($rows as $row) {$cells = $row->getElementsByTagName("td");$cellData = array();foreach ($cells as $cell) {$cellData[] = $cell->generate();}$data[] = $cellData;}echo "<pre>" . print_r($data, true) . "</pre>"; Output / example is at http://tomsfreelance.com/DOMe/DOM_Import.php Make sure you get the updated code at http://tomsfreelance.com/DOMe/DOMe.phps Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415810 Share on other sites More sharing options...
cobusbo Posted March 1, 2013 Author Share Posted March 1, 2013 (edited) eish this is greek to me and according to the above code it seems its its to extract not to delete info Edited March 1, 2013 by cobusbo Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415811 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 cobusbo: If you look at the extra message generated by the var_dump, and then look at what you're code is trying to do on the line above, you should be able to spots why things aren't working. teynon: You might want to update that class of your to more current standard, instead of the age old PHP4 syntax used. Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415813 Share on other sites More sharing options...
cobusbo Posted March 1, 2013 Author Share Posted March 1, 2013 it has to do with the length but how do I fix it thats my question? Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415816 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 If we take a look at this line: $masterContent = $masterElement->getElementsByTagName('users')->item(3);You're trying to fetch the third item of a list, a list which we just determined was empty. Thus it returns null, and it results in you trying to pass null to a method which expects an object. How to solve it involves some basic logic, which I am sure you can figure out on your own if you just think about it. There are many ways to test for what you want, after all. Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415834 Share on other sites More sharing options...
cobusbo Posted March 1, 2013 Author Share Posted March 1, 2013 na i give up il just go do it manualy each time because I got no clue what to do call me stupid sorry Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415844 Share on other sites More sharing options...
salathe Posted March 1, 2013 Share Posted March 1, 2013 $masterElement->getElementsByTagName('users') This is trying to find all <users> elements that are descendants of the $masterElement. The $masterElement is representing the single <users> element that wraps the whole document: it has no descendant <users> elements, only <user>, <name>, and <score> elements. It looks like you wanted to use $masterElement->getElementsByTagName('user'). Then you used ->item(3). Your XML document has three <user> elements so, assuming you change the call to getElementsByTagname() as shown above, you could access those via item(0), item(1), and item(2) respectively. There is no fourth <user> element to get using item(3). It looks like you wanted to use ->item(2). So given those changes to that one line of code, the code posted in post #5 above would work (it would delete the cobus user) and would not present an error. Could you give it a try and let us know how you get on? Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415886 Share on other sites More sharing options...
teynon Posted March 1, 2013 Share Posted March 1, 2013 (edited) Here's how you could use the original script I posted. Example results at: http://tomsfreelance.com/DOMe/DOM_Import2.php (view the source to view the xml output) <?php require_once("DOMe.php"); $dom = new DOMe(""); $dom->importHTML(file_get_contents("file2.html")); echo $dom->generate(); $users = $dom->getElementsByTagName("users"); $data = array(); foreach ($users as &$userGroup) { $elements = $userGroup->getElementsByTagName("user"); foreach ($elements as $user) { $userGroup->remove($user); } } echo $dom->generate(); Edited March 1, 2013 by teynon Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415910 Share on other sites More sharing options...
teynon Posted March 1, 2013 Share Posted March 1, 2013 (edited) cobusbo: If you look at the extra message generated by the var_dump, and then look at what you're code is trying to do on the line above, you should be able to spots why things aren't working. teynon: You might want to update that class of your to more current standard, instead of the age old PHP4 syntax used. 1) Why would I change something that works? 2) What portions are you referring to? If you are referring to the &$'s, I originally set this up because someone was using a PHP4 server and I didn't have any decent DOM's. I pulled in some PHP 5 features, but I don't see any major reason why I should remove the &$'s. Edited March 1, 2013 by teynon Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415911 Share on other sites More sharing options...
cobusbo Posted March 2, 2013 Author Share Posted March 2, 2013 Ok I see I tried it and gets the result, but it doesn't seem to remove the info, and is there a method to delete all records and not each one seperately because its difficult to determine how many there will be... Skuilnaam110cobus5cobusbo4 This is trying to find all <users> elements that are descendants of the $masterElement. The $masterElement is representing the single <users> element that wraps the whole document: it has no descendant <users> elements, only <user>, <name>, and <score> elements.It looks like you wanted to use $masterElement->getElementsByTagName('user').Then you used ->item(3). Your XML document has three <user> elements so, assuming you change the call to getElementsByTagname() as shown above, you could access those via item(0), item(1), and item(2) respectively. There is no fourth <user> element to get using item(3).It looks like you wanted to use ->item(2).So given those changes to that one line of code, the code posted in post #5 above would work (it would delete the cobus user) and would not present an error. Could you give it a try and let us know how you get on? Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415969 Share on other sites More sharing options...
cobusbo Posted March 2, 2013 Author Share Posted March 2, 2013 Thank you but it shows the records after you run it but how do you delete it? and secondly if I look at your script part require_once("DOMe.php"); $dom = new DOMe(""); $dom->importHTML(file_get_contents("file2.html")); why do you open 2 files DOMe.php and get the file contents from file2.html, what is in both documents because at the moment I only got my leaders.xml file and a file named cron.php to delete my records. Here's how you could use the original script I posted. Example results at: http://tomsfreelance.com/DOMe/DOM_Import2.php (view the source to view the xml output) <?php require_once("DOMe.php"); $dom = new DOMe(""); $dom->importHTML(file_get_contents("file2.html")); echo $dom->generate(); $users = $dom->getElementsByTagName("users"); $data = array(); foreach ($users as &$userGroup) { $elements = $userGroup->getElementsByTagName("user"); foreach ($elements as $user) { $userGroup->remove($user); } } echo $dom->generate(); Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415970 Share on other sites More sharing options...
cobusbo Posted March 2, 2013 Author Share Posted March 2, 2013 Ok I found the solution but theres something else that is bugging me now <?php $retriever = new DOMDocument(); $retriever ->load( 'res/leaders.xml' ); $masterElement = $retriever->documentElement; $masterContent = $masterElement->getElementsByTagName('user')->item(0); $oldContent = $masterElement->removeChild($masterContent ); echo $retriever->saveXML(); echo $retriever->save( 'res/leaders.xml' ); ?> This delete each record as I refresh it each time, isnt there a way to delete all at once or create a table where I can tick off to delete each record? and after I deleted all the records isn't there a way to tell me there are no more records and link me back to the index? in place of getting the error Catchable fatal error: Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in /srv/disk1/1296487/www/rekvasvra.uni.me/Gr10/Rente/cron.php on line 23 when all records are deleted Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415976 Share on other sites More sharing options...
Christian F. Posted March 2, 2013 Share Posted March 2, 2013 What you're looking for is count and if, basic language constructs. I strongly recommend that you go back to the basics, and learn about PHP from the start. That way you'll be able to solve basic logical problems like this on your own. Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1415978 Share on other sites More sharing options...
cobusbo Posted March 2, 2013 Author Share Posted March 2, 2013 I cant work like this I need practical examples those pages is to difficult Quote Link to comment https://forums.phpfreaks.com/topic/275072-delete-info-in-xml-document-via-php/#findComment-1416009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.