-2

Possible Duplicate:
A simple program to CRUD node and node values of xml file

I would like to change some data in a xml file with php.

So in my xml file I have this :

 <items><item href="p002.xhtml" id="p002" media-type="application/xhtml+xml"/>
 <item href="img/b002.jpg" id="b002" media-type="image/jpeg"/>
 <item href="p003.xhtml" id="p003" media-type="application/xhtml+xml"/>
 <item href="img/b003.jpg" id="b003" media-type="image/jpeg"/>
 <item href="p004.xhtml" id="p004" media-type="application/xhtml+xml"/>
 <item href="img/b004.jpg" id="b004" media-type="image/jpeg"/>
 <item href="p005.xhtml" id="p005" media-type="application/xhtml+xml"/>
 <item href="img/b005.jpg" id="b005" media-type="image/jpeg"/></items>

I would like to open the file, delete all item greater than 0003 and save changes. I have some tags between this extract.

Result:

 <items><item href="p002.xhtml" id="p002" media-type="application/xhtml+xml"/>
 <item href="img/b002.jpg" id="b002" media-type="image/jpeg"/>
 <item href="p003.xhtml" id="p003" media-type="application/xhtml+xml"/>
 <item href="img/b003.jpg" id="b003" media-type="image/jpeg"/></items>

Trying :

if( ! $xml = simplexml_load_file("pack.xml") ) { 
    echo 'unable to load XML file'; 
} 
else { 
echo "<ul>";
    foreach($xml->items->item as $v ){
        //echo "<li>".$v[href]."</li>";
        if($v[href]=="p003.xhtml" || $v[href]=="img/bg003.jpg"){
            echo "<li>".$v[href]."</li>";
        }

    }
echo "</ul>";
} 

How to check the greater than p003.xhtml and img/bg003.jpg ?

Community
  • 1
  • 1
Seb Gy
  • 140
  • 1
  • 5
  • 15

1 Answers1

1

You are going to want to parse the file using simplexml. This will generate a nice set of DOM like classes for you to work with. From these classes you can search for elements with your criteria and remove them before saving the xml tree back to text

secretformula
  • 6,414
  • 3
  • 33
  • 56