Update XML Node via PHP...

Reply

Join Date: Apr 2008
Posts: 31
Reputation: Mapper99 is an unknown quantity at this point 
Solved Threads: 0
Mapper99 Mapper99 is offline Offline
Light Poster

Update XML Node via PHP...

 
0
  #1
Aug 11th, 2009
Hi there,

I am trying to update a node in an XML file using PHP.

Here is the XML:

  1. <users>
  2. <user>
  3. <id>1</id>
  4. <firstname>Bob</firstname>
  5. <lastname>Marley</lastname>
  6. </user>
  7. <user>
  8. <id>2</id>
  9. <firstname>Bruce</firstname>
  10. <lastname>Springsteen</lastname>
  11. </user>
  12. </users>

How can I go about searching the XML file for all tags where "id=2" and setting the firstname and lastname nodes to new values?

Any replies would be greatly appreciated!

Mapper
Last edited by peter_budo; Aug 11th, 2009 at 2:50 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,405
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Update XML Node via PHP...

 
0
  #2
Aug 11th, 2009
http://php.net/SimpleXML, You might want to take a look at the xpath stuff.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Mapper99 is an unknown quantity at this point 
Solved Threads: 0
Mapper99 Mapper99 is offline Offline
Light Poster

Re: Update XML Node via PHP...

 
0
  #3
Aug 11th, 2009
Originally Posted by ShawnCplus View Post
http://php.net/SimpleXML, You might want to take a look at the xpath stuff.
I have taken a brief look into SimpleXML and have created the following. I just can't get the XML to update:

  1. $file = 'officedata.xml';
  2. $xml = simplexml_load_file($file);
  3. $lastname = $xml->xpath('//user[id="2"]/lastname');
  4. $xml_string='';
  5.  
  6. while(list( , $node) = each($lastname))
  7. {
  8.  
  9. $xml_string .= $node->asXML();
  10. $xml_string='newlastname';
  11. }
  12.  
  13. echo $xml_string;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC