addChild and attribute with simpleXml


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. header('Content-type: text/xml');
  3.  
  4. // first <winner id="1" /> node
  5. $xml =
  6. '<win visit="0">
  7. <winner id="1" />
  8. </win>';
  9. $xml = simplexml_load_string($xml);
  10.  
  11. // second node
  12. $newNode = $xml->addChild("winner");
  13. $newNode->addAttribute("id","2");
  14.  
  15. // three node
  16. $newNode = $xml->addChild("winner");
  17. $newNode->addAttribute("id","3");
  18.  
  19. echo $xml->asXML();
  20. ?>
  21.  
  22. // RESULT
  23. <win visit="0">
  24. <winner id="1"/>
  25. <winner id="2"/>
  26. <winner id="3"/>
  27. </win>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.