1.使用new关键字创建
$xml="
- 1aaa16
- 2bbb26
";
$rss=new SimpleXMLElement($xml);
2.使用simplexml_load_string()创建
$xml="
- 1aaa16
- 2bbb26
";
$rss=simplexml_load_string($xml);
3.使用simplexml_load_file()从一个URL创建
$xml="
- 1aaa16
- 2bbb26
";
$rss=new SimpleXMLElement($xml);
foreach($rss->item as $v){
echo $v->name,'
';
}
echo $rss->item[1]->age;//读取数据
echo '
';
$rss->item[1]->name='ccc';//修改数据
foreach($rss->item as $v){
echo $v->name,'
';//aaa
ccc
}
echo '
';
unset($rss->item[1]);//输出数据
foreach($rss->item as $k=>$v){
echo $v->name,'
';//aaa
}
echo '
';
//添加数据
$item=$rss->addChild('item');
$item->addChild('id','3');
$item->addChild('name','ccc_new');
$item->addChild('age','40');
foreach($rss->item as $k=>$v){
echo $v->name,'
';//aaa
ccc_new
}
$rss->asXML('personinfo.xml');
?>