-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 ?

1
  • I know to open select delete but I don't know how to select item greater than 0003 Commented Jun 21, 2012 at 12:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.