SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating XML nodes using SimpleXML adds extra node

    I have a very simple script that basically searches an xml file for a node based on the id of another node. I then update the node I found with a new value. For some reason my code is not performing the update. It is actually creating another node within the existing node. Any idea why?

    XML:
    Code:
      <?xml version="1.0" encoding="UTF-8" ?> 
    - <users>
    - <user>
      <id>1</id> 
      <firstname>Bob</firstname> 
      <lastname>Marley</lastname> 
      </user>
    - <user>
      <id>2</id> 
      <firstname>Bruce</firstname> 
      <lastname>Springsteen</lastname> 
      </user>
      </users>
    PHP Code:

    Code:
    <?php
    
    $file = "officedata.xml";
    
    $xml=simplexml_load_file($file);
    
    foreach ($xml->xpath('//user[id="2"]/lastname') as $desc) {
    echo "$desc\n";
    $desc->lastname = "Penner"; 
    }
    
    file_put_contents($file, $xml->asXML()); 
    
    ?>
    The updated XML looks like this:

    Code:
      <?xml version="1.0" encoding="UTF-8" ?> 
    - <users>
    - <user>
      <id>1</id> 
      <firstname>Bob</firstname> 
      <lastname>Marley</lastname> 
      </user>
    - <user>
      <id>2</id> 
      <firstname>Bruce</firstname> 
    - <lastname>
      Springsteen 
      <lastname>Penner</lastname> 
      </lastname>
      </user>
      </users>

  2. #2
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, that code outputs:

    Springsteen

    and that is correct according to your code.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file_put_contents($file, $xml->asXML());

    outside the loop?

    Isn't your wrong output correct in the way you do it?

    Is it easier to use DOM XML functions like create_element, append_child ... ?

  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the result is correct for how I have coded this script. I am just not happy with the result (two lastname tags). Perhaps I will try and drop the node an recreate it...just seems like there should be a better way to update XML via PHP!

  5. #5
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Moss, Norway.
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. I don't agree. What does that code line do outside the loop? Print out variables and var_dump objects.
    2. Some times it is better easier / more transparent to use DOM functions as indicated in my last post.

  6. #6
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    You mean something like this?

    Code:
    $file = "officedata.xml";
    
    $xml=simplexml_load_file($file);
    
    foreach ($xml->xpath('//user[id="2"]/lastname') as $desc) {
    
    $dom=dom_import_simplexml($desc);
    $dom->nodeValue = "Penner"; 
    
    }
    
    file_put_contents($file, $xml->asXML());
    Which works like a charm...Thanks!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •