Jump to content

Delete info in xml document via php


cobusbo

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 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?

Link to comment
Share on other sites

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:

 

<?php
require_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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

 

$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? :shy:

Link to comment
Share on other sites

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 by teynon
Link to comment
Share on other sites

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 by teynon
Link to comment
Share on other sites

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? :shy:

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.