dtest84004 2014-09-27 00:21
浏览 68
已采纳

simpleXML从CDATA获取值[重复]

This question already has an answer here:

I'm trying to get values from CDATA which are inside <b></b>. with simpleXML, but so far without any good results. Here is some part of my xml file -

<item>
<title>
<![CDATA[
Bez starpniekiem tiek izīrēts pilnībā mēbelēts 1-istabu dzīvoklis 5. stāvā uz ilgu laiku. Dzīvoklis mēbelēts, ar iebūvētu vir ...
]]>
</title>
<link>
http://www.ss.lv/msg/lv/real-estate/flats/riga/centre/abhkp.html
</link>
<pubDate>Thu, 25 Sep 2014 02:59:55 +0300</pubDate>
<description>
<![CDATA[
<a href="http://www.ss.lv/msg/lv/real-estate/flats/riga/centre/abhkp.html"><img align=right border=0 src="http://i.ss.lv/images/2014-09-24/348773/VHkBG09gR1s=/1.t.jpg" width="160" height="120" alt=""></a>
 District: <b><b>centrs</b></b><br/>Street: <b><b>Klijānu 2</b></b><br/>Rooms: <b><b>1</b></b><br/>m2: <b><b>35.00</b></b><br/>Type: <b><b>Renov.</b></b><br/>: <b><b>8.57</b> €</b><br/>Price: <b><b>300</b> €/mēn.</b><br/><br/><b><a href="http://www.ss.lv/msg/lv/real-estate/flats/riga/centre/abhkp.html">Apskatīt sludinājumu</a></b><br/><br/>
]]>
</description>
</item>

I know how to get values from this xml file like title, pudDate, link, but I don't know how can I get values from description tag so I can add them into database sorted by Price, District, Type, Image.

So far I tryed to save description tag into string and after that using explode() cut out parts with info I need, I have the right values but they come with tags. Some with tags.

This is what I was trying -

$url = "http://www.ss.lv/lv/real-estate/flats/riga/hand_over/rss/";

$result = simplexml_load_file($url);

foreach ($result->channel->item as $item) {
    $title =(string)$item->title;
    description = (string)$item->description;
    $link = $item->link;
    $pubDate = $item->pubDate;

// Cut out from description price
    $parts = explode("Price: ", $description);
    $pri= "";
    for ($i = 1; $i < 2; $i++) {
        $pri= $parts[$i];
    }
    $parts2 = explode("</b>", $pri);
    for ($i = 1; $i < 2; $i++) {
        $price= $parts2[0];
    }

but I think my solution is absoloutly wrong and the result of cutting is - <b><b>300 or <b>650

so my question is: how can I get clean values out of my CDATA using something similar to

$pubDate = $item->pubDate

using something like that?

$description = (string)$item->description->b[0] - to get right values from CDATA.

</div>
  • 写回答

1条回答 默认 最新

  • douyun1950 2014-09-27 00:25
    关注

    In your simplexml_load_file(), you need to add the parameter LIBXML_NOCDATA flag:

    $url = "http://www.ss.lv/lv/real-estate/flats/riga/hand_over/rss/";
    $result = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
                                                          // ^^ here
    foreach($result->channel->item as $item) {
        $title = (string) $item->title;
        $desc = (string) $item->description;
        $dom = new DOMDocument($desc);
        $dom->loadHTML($desc);
        $bold_tags = $dom->getElementsByTagName('b');
        foreach($bold_tags as $b) {
            echo $b->nodeValue . '<br/>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型