• 解析xml文件,后两个参数解析CDATA使用
$xml = simplexml_load_file('xml.xml',"SimpleXMLElement",LIBXML_NOCDATA);
$xml=simplexml_load_string($note);解析字符串同上

$x = simplexml_load_file("1.xml","SimpleXMLElement",LIBXML_NOCDATA);
foreach ($x->month as $k=>$v){
    $data[]=$v;
}
dump((array)$data[0]);
  • 生成RSS2.0
$xml='<?xml version="1.0" encoding="utf-8"?><rss></rss>';
$r  = new SimpleXMLElement($xml);
$r->addAttribute('version', '2.0');
$channel = $r->addChild('channel');
$channel->addChild("title","标题");
$channel->addChild("description","<![CDATA[描述]]>");
$channel->addChild("link","https://www.baidu.com");
$channel->addChild("language","zh-CN");
$channel->addChild("pubDate",date("c",time()));
$image = $channel->addChild("image");

$image->addChild("link","https://www.baidu.com");
$image->addChild("url","https://www.baidu.com/img/baidu_jgylogo3.gif");
$image->addChild("title","图片标题");
$image->addChild("description","<![CDATA[描述]]>");

$arr[] = array(
    'id'=>1,
    'title'=>'标题就是我',
    'type'=>'分类',
    'content'=>'我就是内容',
);
$arr[] = array(
    'id'=>2,
    'title'=>'标题就是我',
    'type'=>'分类',
    'content'=>'我就是内容',
);
$arr[] = array(
    'id'=>3,
    'title'=>'标题就是我',
    'type'=>'分类',
    'content'=>'我就是内容',
);
$arr[] = array(
    'id'=>4,
    'title'=>'标题就是我',
    'type'=>'分类',
    'content'=>'我就是内容',
);

foreach ($arr as $rs){
   $item =  $channel->addChild("item");
   $item->addChild("title",$rs['title']);
    $item->addChild("link","https://www.baidu.com?id=".$rs['id']);
    $item->addChild("type",$rs['type']);
    $item->addChild("description","<![CDATA[".$rs['content']."]]>");
    $item->addChild("pubDate",date("c",time()));
    $item->addChild("author",'作者');
}
header("Content-Type:text/xml");
$str = $r->asXML();
echo $str;
  • 生成xml
$xml='<?xml version="1.0" encoding="utf-8"?><year></year>';
$r  = new SimpleXMLElement($xml);
foreach(range(1,12) as $k)
{
   $m =  $r->addChild("month");
   $m->addAttribute("name",$k);
    $m->addAttribute("name1-qq",$k);
   $m->addChild("month",$k);
  $d =  $m->addChild("day",17);
  $d->addAttribute("xml:lan","language","www");//命名空间

}
header("Content-Type:text/xml");
$str = $r->asXML();
echo $str;
文档更新时间: 2020-09-17 03:02   作者:Yoby