0

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.

Santar
  • 61
  • 10

1 Answers1

2

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/>';
    }
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks. It works, but after first loop I got every next value x2. But if I'm saving values to the database I can simply run database check if data already exists and then if they exists then don't write them, right? – Santar Sep 27 '14 at 12:16
  • @Santar yes of course you could do that, if there are some duplicates, but i think just too much of a overhead, you could also put them inside an array, then in the end of the inner loop after that use an `array_unique()` so that you don't need to go check each time in DB, that would be much preferable – Kevin Sep 27 '14 at 12:40
  • Can I ask you last question? How can I get only prices from $b? I red all ower the Stackoverflow to use `item(x)`, bur when I write echo `$b->nodeValue->item(2)` or `$b->item(2)->nodeValue` it doesnt work – Santar Sep 30 '14 at 23:07
  • @Santar okay i'l get back to you a little bit – Kevin Sep 30 '14 at 23:57
  • hello @Santar could you setup another question for this question again, i'd like to answer this on a separate question, its very tricky – Kevin Oct 01 '14 at 00:54
  • ok. http://stackoverflow.com/questions/26131148/foreach-xml-node-return-selected-element – Santar Oct 01 '14 at 01:14