//non namespaced attributes
<?php
$data=<<<XML
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<item>
<title>Title: 0</title>
<link test="test" test2="This is a test">Link:0</link>
<media:thumbnail url="0"/>
<media:content url="0" type="video/mp4" />
</item>
<item>
<title>Title: 1</title>
<link>Link:1</link>
<media:thumbnail url="1"/>
<media:content url="1" type="video/mp4" />
</item>
<item>
<title>Title: 2</title>
<link>Link:2</link>
<media:thumbnail url="2"/>
<media:content url="2" type="video/mp4" />
</item>
<item>
<title>Title: 3</title>
<link>Link:3</link>
<media:thumbnail url="3"/>
<media:content url="3" type="video/mp4" />
</item>
</channel>
</rss>
XML;
$xml=simplexml_load_string($data);
echo $xml->channel[0]->item[0]->title."<br>";
echo $xml->channel[0]->item[0]->link->attributes()->test2="This is a NEW test!.";
//Creates update.xml with updated test2 value
$xml->asXml('updated.xml');
?>
===============================================================================
//namespaced attributes
<?php
$data=<<<XML
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<item>
<title>Title: 0</title>
<link test="test" test2="This is a test">Link:0</link>
<media:thumbnail url="Thumbnail URL: 0"/>
<media:content url="Content URL: 0" type="video/mp4" />
</item>
<item>
<title>Title: 1</title>
<link>Link:1</link>
<media:thumbnail url="1"/>
<media:content url="1" type="video/mp4" />
</item>
</channel>
</rss>
XML;
$xml=simplexml_load_string($data);
//reading simple node
echo $xml->channel[0]->item[0]->title;
echo "<br>----------------------------------<br>";
//reading/updating simple node's attribute
echo $xml->channel[0]->item[0]->link->attributes()->test2="This is a NEW test!.";
echo "<br>----------------------------------<br>";
//reading/updating namespaced node's attribute
echo $xml->channel[0]->item[0]->children('media',TRUE)->content->attributes()->type="VIDEO/MP6";
//saving...
$xml->asXml('updated.xml');
?>
Comments
Comments are not available for this entry.