Connecting Tech Pros Worldwide Forums | Help | Site Map

Modifying an XML file with PHP

mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 188
#1: Jun 19 '09
I have an XML file representing categories like this
Expand|Select|Wrap|Line Numbers
  1. <?xml version='1.0' encoding='utf-8' ?>
  2. <bank>
  3. <img>
  4.     <src>img/GF0813-1_thumb.jpg</src>
  5.     <title>classicΙ</title>
  6.     <caption>Click here</caption>
  7.     <link>main.php?page=2&amp;id=125</link>
  8.     <target>_self</target>
  9. </img>
  10. <img>
  11.     <src>img/PBE0720008_thumb.jpg</src>
  12.     <title>BULLETS</title>
  13.     <caption>Click here</caption>
  14.     <link>main.php?page=2&amp;id=117</link>
  15.     <target>_self</target>
  16. </img>
  17. </bank>
  18.  
where each <img></img> represents a category which has an image (<src>), title (<title>), caption (<caption>), link and target of that link.
Now when I add a category its easy to open the xml like an ordinary file and add the corresponding <img> at the end, but when I delete/modify a category I have to check where the corresponding <img></img> exists (each category/<img> has a unique id going into the <link>) and modify that how can I do it? (My initial idea involves using explode with </img>, is there a better way to handle xml through PHP?)

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,650
#2: Jun 19 '09

re: Modifying an XML file with PHP


Quote:

Originally Posted by mikek12004 View Post

and modify that how can I do it? (My initial idea involves using explode with </img>, is there a better way to handle xml through PHP?)

of course. PHP comes with a bundle of XML manipulation functionality:
SimpleXML
DOM
SAX type parsers (XMLReader, XMLWriter, XML Parser)
Reply