doue8385 2015-03-01 03:49
浏览 48
已采纳

如何使用PHP + mySQL从XML中获取所有节点?

I need to grab all nodes from XML. After I try use below PHP coding. It is work. But I can not grab <description> and image from xml.

Here is XML from others website (UTF-8 Thai Language) http://www.dailynews.co.th/rss/rss.xml

Here is my PHP code :

$Rsssurl_SQL="SELECT * from rss_feed_url"; 
$Rsssurl_RESULT=mysql_db_query($dbname,$Rsssurl_SQL);
while ($Rsssurl_ROW=mysql_fetch_array($Rsssurl_RESULT)) {
$request_url = $Rsssurl_ROW[1];
$xml = simplexml_load_file($request_url) or die("");
foreach($xml->channel->item as $item){
$title = $item->title;   
$content = $item->description;
$date = $item->pubDate;   
$link = $item->link;

/* I use ereg_replace to replace special characters */

$content=ereg_replace("&lt;","<",$content);
$content=ereg_replace("&gt;",">",$content);
$content=ereg_replace("&amp;","&",$content);
$content=ereg_replace("&quot;","\"",$content);
$content=ereg_replace("&#039;","\'",$content);

$title=ereg_replace("&lt;","<",$title);
$title=ereg_replace("&gt;",">",$title);
$title=ereg_replace("&amp;","&",$title);
$title=ereg_replace("&quot;","\"",$title);
$title=ereg_replace("&#039;","\'",$title);
$title=ereg_replace("\'","",$title);
$title=ereg_replace("<","", $title);
$title=ereg_replace(">","", $title);
$title=ereg_replace("&","", $title);
$title=ereg_replace("\"","", $title);
}
}

I can grab <title>, <link> , <pubDate> and insert all to mySQL. But only <description> I can not grab it all (image and long text).

I try to search from many helps and read from W3C school. I can not found exactly answers.

Please , helps. Thanks.

  • 写回答

1条回答 默认 最新

  • duanqiang9212 2015-03-01 04:16
    关注

    You need to cast the value as a string when using simplexml. Try this :

    $Rsssurl_SQL="SELECT * from rss_feed_url"; 
    $Rsssurl_RESULT=mysql_db_query($dbname,$Rsssurl_SQL);
    while ($Rsssurl_ROW=mysql_fetch_array($Rsssurl_RESULT)) {
        $request_url = $Rsssurl_ROW[1];
        $xml = simplexml_load_file($request_url) or die("");
        foreach($xml->channel->item as $item){
            $title = (string) $item->title;   
            $content = (string) $item->description;
            $date = (string) $item->pubDate;   
            $link = (string) $item->link;
        }
    }
    
    • Sorry , I forget } at the end.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测