943,625 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2699
  • PHP RSS
Aug 11th, 2009
0

Update XML Node via PHP...

Expand Post »
Hi there,

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

Here is the XML:

xml Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008
Aug 11th, 2009
0

Re: Update XML Node via PHP...

http://php.net/SimpleXML, You might want to take a look at the xpath stuff.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Aug 11th, 2009
0

Re: Update XML Node via PHP...

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
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:

PHP Syntax (Toggle Plain Text)
  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;
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Questions about passing variable with URL
Next Thread in PHP Forum Timeline: Dynamic Login





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC