| | |
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: 30
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: 30
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 |
actionscript3 adobe ajax ajaxexample api array asp autosuggest beginner blogger blogging c++ class cms code combobox curl database delete development display dynamic ecommerce email error errorlog explodefunction external file files flash form forms function functions gdata google html image images include java javascript joomla jquery keywords lamp limit link linux local login mail matching menu mobile multiple mysql news nodes oop paging php post programming quality query random root rss ruby script search security select sessions sharepoint sms snippet software source spam sql swf table tannenbaum text tutorial upload vb2008 vbulletin video w3c web xml xsl xslt y!os youtube zend






