PHP XML editing attributes

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

So I've found plenty of resources to help light a fire under my arse and start a simple cms system I'm creating for a client. It's an extremely lightweight cms so I'll be using xml instead of a database.

This will be my first venture with PHP so excuse my noobiness on the subject. I'm fairly confident that I've found enough resources to figure out how to use PHP to write to an xml file and read one. However, I'm having trouble finding information on editing xml node attributes. Seems there's plenty of information on creating xml nodes and traversing the xml nodes. But say I want to write an attribute like so:
XML Code: [ Select ]
<parent>
 <child name="little one">Text stuff</child>
</parent>
 
  1. <parent>
  2.  <child name="little one">Text stuff</child>
  3. </parent>
  4.  


So if I want to change the "name" attribute to contain "bigger one" instead of "little one", how would I go about doing that?

I'm not planning on using simpleXML for this, just straight php, but am willing to do so if need be.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

PHP Code: [ Select ]
<?php
 
$xml              = simplexml_load_string('<parent><child name="little one">Text stuff</child></parent>');
$nodes            = $xml->xpath('/parent/child');
$nodes[0]['name'] = 'big one';
 
echo $xml->asXML();
 
?>
  1. <?php
  2.  
  3. $xml              = simplexml_load_string('<parent><child name="little one">Text stuff</child></parent>');
  4. $nodes            = $xml->xpath('/parent/child');
  5. $nodes[0]['name'] = 'big one';
  6.  
  7. echo $xml->asXML();
  8.  
  9. ?>


There's another example on the manuals basic XML examples page. Example #9 is the one you're looking for. :)
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

awesome, thanks jb.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 64 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.