View Single Post
Old 11-17-2012, 03:35 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,365
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
I have fallen in love with simplexml. It does everything you need to except delete a section of code. I found that out trying to answer this for you. I next went to the old DOMDocument method to delete but had trouble identifying the item number of the section I needed to delete. (There maybe a way of finding this in the doc, but I can't do it.) So I combined the two. First the simplexml to ID the section number by "category == "switch"" or something like it. and then using the dom to delete the section.

I did it by category == "switch" but you can use category == "name". simplexml is easy to use.

PHP Code:
<?php
$xml 
simplexml_load_file("products.xml");
$i 0;
foreach (
$xml as $category[])
{
    if(
$category[$i]->category == "switch")
        
$itemNumber $i;
    
$i++;
}

$xmlDOC = new DOMDocument();
$xmlDOC ->load('products.xml');
$YourElement $xmlDOC->documentElement ;
$Element2Remove $YourElement->getElementsByTagName('product')->item($itemNumber);

$oldContent $YourElement->removeChild($Element2Remove);
$xmlDOC->save("temp.xml"); // OR whatever file you want.
?>
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
chumroo (11-27-2012)