delete info in xml doc via php

Started on the old forum in March 2013 · view the original in the archive · old links redirect here

5 replies · 683 views · started 13 years ago · 1 min read

4 participants
cobusbo Developer · 117 posts #1

Hi guys I need some help I have a xml document with the following info

<?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 a php script to dump rows in a table with coding to remove the info in the xml document but I cant seem to get it to work it just shows me the rows but dont delete them

<? 
 $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('user')->item(0);

$oldContent = $masterElement->removeChild($masterContent );

echo $retriever->saveXML();

?>

can someone help me to get my php document correct so it can clean my xml document to just show the xml doc with the following info in it afterwards

<?xml version="1.0" encoding="UTF-8"?>
<users>
 <</users>

Added after 21 minutes:

cobusbo wrote: Hi guys I need some help I have a xml document with the following info

<?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 a php script to dump rows in a table with coding to remove the info in the xml document but I cant seem to get it to work it just shows me the rows but dont delete them

<? 
 $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('user')->item(0);

$oldContent = $masterElement->removeChild($masterContent );

echo $retriever->saveXML();

?>

can someone help me to get my php document correct so it can clean my xml document to just show the xml doc with the following info in it afterwards

<?xml version="1.0" encoding="UTF-8"?>
<users>
 <</users>

ok I manage to get it to work but now I got some other help needed

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

shushant Developer · 207 posts #2
   print_r(
     json_decode( json_encode( simplexml_load_file('xmlfile.xml'), true));

          )
cobusbo Developer · 117 posts #3

shushant wrote:

   print_r(
     json_decode( json_encode( simplexml_load_file('xmlfile.xml'), true));


          )

what does this do?

just_m3. Developer · 199 posts #4

cobusbo wrote: what does this do?

it transforms your xml into array and you can acces values by name

ex:

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

Attachments: hJloV7K.png
cobusbo Developer · 117 posts #5

just_m3. wrote: it transforms your xml into array and you can acces values by name

ex:

ok so where should I include it into my code witch line?

kevk3v Senior · 332 posts #6

Don't know what this is lol but try

$newdata = '<?xml version="1.0" encoding="UTF-8"?>
<users>
</users>';

$xmldoc = 'XML DOCUMENT NAME...';

chmod($xmldoc,0777)
file_put_contents($xmldoc,$newdata);

Add after mysql_query($query); in a new line

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close