| | |
Delete XML nodes failing...
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 31
Reputation:
Solved Threads: 0
Hi there,
I am trying to delete XML nodes using PHP. Here is a sample of my xml file.
Here is my code so far:
Any idea why this won't work? If I remove the IF statement, all of the XML data gets removed.
Thanks in advance,
Mapper
I am trying to delete XML nodes using PHP. Here is a sample of my xml file.
PHP Syntax (Toggle Plain Text)
<users> <user> <fullname>PC1</fullname> <flooor>4</floor </user> <user> <fullname>PC2</fullname> <flooor>3</floor </user> </users>
Here is my code so far:
PHP Syntax (Toggle Plain Text)
<?php $users = new DOMDocument(); $users->load("officedata.xml"); $suser = simplexml_load_file("officedata.xml"); $count = 0; foreach($suser->user as $user) { if ($user['fullname'] == "PC1") { $users->documentElement->removeChild($users- documentElement->childNodes->item($count)); $count--; } $count++ } $users->save("officedata.xml"); ?>
Any idea why this won't work? If I remove the IF statement, all of the XML data gets removed.
Thanks in advance,
Mapper
•
•
Join Date: Apr 2008
Posts: 31
Reputation:
Solved Threads: 0
For those of you could not figure this one out, after a few tries I got it:
Works like a charm and is a very useful code snippet for deleting XML records using PHP!
Mapper
<FAKE SIGNATURE>
PHP Syntax (Toggle Plain Text)
$users = new DOMDocument(); $users->load("officedata.xml"); $suser = simplexml_load_file("officedata.xml"); $count = 0; $user = $users->getElementsByTagName("user"); foreach($user as $value) { $count++; $tasks = $value->getElementsByTagName("fullname"); $task = $tasks->item(0)->nodeValue; if ($task == "PC1") { $users->documentElement->removeChild($users->documentElement->childNodes->item($count)); } } $users->save("officedata.xml"); ?>
Works like a charm and is a very useful code snippet for deleting XML records using PHP!
Mapper
<FAKE SIGNATURE>
Last edited by peter_budo; Oct 2nd, 2009 at 10:34 am. Reason: Keep In The Site - No Fake signatures. Create a sitewide signature within the user control panel.
![]() |
Similar Threads
- swap xml nodes (RSS, Web Services and SOAP)
- Need Help TXT to XML Converter (VB.NET)
- Adding new node (XML, XSLT and XPATH)
- C# XMLDataSource - filtering XML (ASP.NET)
- Read/Edit specific .xml nodes in vb6 (Visual Basic 4 / 5 / 6)
- Adding Attributes to all XML Nodes (RSS, Web Services and SOAP)
- mobile platfrom web 2.0 delete xml post (ASP.NET)
- compare 2 xml files with csharp (C#)
Other Threads in the PHP Forum
- Previous Thread: Multiple Files Upload to Server
- Next Thread: Image upload problem
| Thread Tools | Search this Thread |
Tag cloud for delete, nodes, php, xml
action add ajax ajaxhelp api array asp autocomplete beginner blogger blogging broken buttons c# c++ class cms code curl data database date delete development display downloader dropdownlist dynamic edit email error file files flash flipbook folder form forms function gdata google html httppost ibm iframe image include integration java javascript job jquery kernel lamp leak limit link linux login mail memory menu multiple mysql mysqlquery news nodes oop paypal php play post programming query rss schools script search security serialization session sms soap spam sql support! tutorial upload validation variable vbulletin video w3c web webdesign xml xsl xslt youtube zend






