| | |
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 |
ajax ajaxcode amf api array beginner blogger blogging class cms code codes curl database date delete developer developers development display dojofoundation dropdown dynamic email error fatalerror file files flash form forms gdata google hosting html image include indentedsubcategory innerjoins insert java javascript jquery lamp limit link linkednodes linspire linux login loops mail menu microsoft mimic multiple multipletables mysql news nodes number oop open outofmemmory pdf persist php popup post programmer programming query rss script search searchmonkey security sms sockets spam sql subscription swappingxmlnodes traffic tutorial txttoxmlconverter upload video virus w3c web webdesign website wpf xml xsl xslt ydn youtube zend






