Editing XML Nodes through SimpleXML PHP -


i know similar questions have been asked several times, can't find solution following:

i've got simple xml file: servers.xml

<servers>      <server>         <name> google </name>         <address>http://www.google.com</address>     </server>      <server>          <name> yahoo </name>         <address>http://www.yahoo.com</address>     </server>      <server>         <name> bing </name>         <address>http://www.bing.com</address>     </server>  </servers> 

now, i'm trying <server> node has name of "google" example, , change address tag. have no idea how go using simplexml. example scenario following:

  1. get server object/array $servername = "google"
  2. edit server's address field different http://www.google.co.uk
  3. write changes xml file.

any appreciated.

  1. get server object/array $servername = "google"

    // array of <server> elements chosen name $googles = $servers->xpath('server[name = " google "]'); 
  2. edit server's address field different http://www.google.co.uk

    //find google , change address $google->address = 'http://www.google.co.uk'; 
  3. write changes xml file.

    $servers->savexml('path/to/file.xml'); 

full example

$servers = simplexml_load_file('path/to/file.xml'); $googles = $servers->xpath('server[name=" google "]'); foreach ($googles $google) {     $google->address = 'http://www.google.co.uk'; } $servers->savexml('path/to/file.xml'); 

more info


Comments