Changing elements and attributes : simplexml_load_string « XML « PHP






Changing elements and attributes

 
<?
$menu=<<<_XML_
<?xml version="1.0" encoding="utf-8" ?>
<rss version="0.91">
 <channel>
  <title>What's For Dinner</title>
  <link>http://example.com/</link>
  <description>choices</description>
  <item>
   <title>Feet</title>
   <link>http://example.com</link>
   <description>test</description>
  </item>
 </channel>
</rss>
_XML_;

$xml = simplexml_load_string($menu);

$xml['version'] = '6.3';
$xml->channel->title = strtoupper($xml->channel->title);

for ($i = 0; $i < 3; $i++) {
    $xml->channel->item[$i]->link = str_replace('menu.example.com',
        'dinner.example.org', $xml->channel->item[$i]->link);
}
?>
  
  








Related examples in the same category

1.Accessing identically named elements
2.Adding attributes
3.Loop through children
4.Loading an XML document from a file
5.Fetching a remote XML document
6.Parsing XML in a string
7.Parsing an XML Document with SimpleXML
8.simplexml_load_file.php
9.Loading a remote XML document
10.Outputting XML
11.Using SimpleXML to extract data
12.Simple XML
13.Print XML element attributes
14.Printing sub-element contents
15.Saving an XML document to a file