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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?