| | |
Delete XML nodes failing...
Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
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. I am just trying to remove "PC1"
Thanks in advance,
Mapper
I am trying to delete XML nodes using PHP. Here is a sample of my xml file.
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<users> <user> <fullname>PC1</fullname> <floor>4</floor> </user> <user> <fullname>PC2</fullname> <floor>3</floor> </user> </users>
Here is my code so far:
XML, XSLT and XPATH 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. I am just trying to remove "PC1"
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
http://www.freegooglewaveinvites.com
XML, XSLT and XPATH 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 == "PCID4") { $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
http://www.freegooglewaveinvites.com
![]() |
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 XML, XSLT and XPATH Forum
- Previous Thread: n XSLT variables to fetch n records from xml file
- Next Thread: how can i read the attribute of xml in c# tree
| Thread Tools | Search this Thread |
actionscript3 add api array asp.net blogger blogging c++ code delete development dynamic edit error file flash flipbook gdata google html kernel linspire linus linux loops memory microsoft news node nodes openoffice optimisation php programming random rss search serialization standards swappingxmlfromflash swappingxmlnodes swf tanenbaum tannenbaum temperature text torvalds transform txttoxmlconverter vb2008 w3c web xml xmlnotloading xmlonserver xsl






