2014-09-30 3 views
0

내 xml cdata 태그에서 선택된 값만 가져 오는 방법은 무엇입니까?foreach XML 노드가 선택된 요소를 반환합니다.

지금까지 유래의 도움으로 내가 문자열

$result = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA); 

    foreach ($result->channel->item as $item) { 
     $desc = $item->description; 
     $dom = new DOMDocument($desc); 
     $dom->loadHTML($desc); 
     $bold_tags = $dom->getElementsByTagName('b'); 
     foreach($bold_tags as $b) { 
      echo $b->nodeValue . "<br>"; 
     } 

의 모든 <b> 태그를 얻을 수 있지만 <b> 안에있는 모든 데이터를 에코하지만 난 단지 얻을의 가격을 말할 수 있도록합니다. 나는 그 값을 얻기 위해 ->item(x)을 사용하는 stackoverflow에 빨간색이 아무것도 작동하지 않습니다. 이것을 echo $b->nodeValue->item(2) . "<br>"; 또는 echo $b->item(2)->nodeValue . "<br>";처럼 넣으면. 그렇다면 어디서 둬야합니까? 또는 <b> 요소를 가격으로 얻으려면 무엇을 사용해야합니까? 가격은 항상 같은 장소에 있습니다. 설명을 위해,

$url = "http://www.ss.lv/lv/real-estate/flats/riga/hand_over/rss/"; 
$result = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA); 

$data = array(); 
foreach($result->channel->item as $item) { 
    $temp = array(); 

    $title = (string) trim($item->title); 
    $desc = $item->description; 

    $temp['title'] = $title; 

    $dom = new DOMDocument('1.0', 'utf-8'); 
    $desc = mb_convert_encoding($desc, 'HTML-ENTITIES', "UTF-8"); 
    $dom->loadHTML($desc); 
    $xpath = new DOMXpath($dom); 
    $price_tag = $xpath->query('//text()[contains(., "Cena")]'); // target Cena, 
    // i didn't know this was PRICE in translation haha 
    $price = $price_tag->item(0)->nextSibling->nodeValue; 
    $temp['price'] = $price; 
    $data[] = $temp ; 
} 

echo '<pre>'; 
print_r($data); 

좋아 : 당신은 그 가격을 분석하기 위해이 방법을 시도 할 수 있습니다

<a href="//www.ss.lv/msg/lv/real-estate/flats/riga/purvciems/deblb.html"> 
    <img align="right" border="0" src="//i.ss.lv/images/2014-10-01/349288/VHkAHkBlRlo=/1.t.jpg" width="160" height="120" alt=""> 
</a> District: <b><b>Purvciems</b></b><br /> 
Street: <b><b>Dudajeva g. 12</b></b><br /> 
Rooms: <b><b>2</b></b><br /> 
m2: <b><b>50</b></b><br /> 
Type: <b><b>LT proj.</b></b><br /> 
: <b><b>3</b> €</b><br /> 
Price: <b><b>150</b> €/mēn.</b><br /> 
<br /> 
<b><a href="//www.ss.lv/msg/lv/real-estate/flats/riga/purvciems/deblb.html">Apskatīt sludinājumu</a></b><br /> 
<br /> 
]]> 
+0

매우 혼란 스럽기 때문에 파싱하기가 어려울 것입니다. [API] (https://www.ss.lv/lv/api/)는 데이터를 가져 오는 데 더 좋은 옵션을 제공합니까? –

+0

@ialarmedalien 모든 API는 내 사이트에 사이트 창을 추가하는 자바 스크립트 코드입니다.이 CDATA에 필요한 모든 정보가 포함 된이 사이트의 RSS 피드를 사용할 수 있습니다. – Santar

+0

@Santar 안녕하세요 santar가 내 대답을 확인하십시오. – Ghost

답변

1

:

를 따라서 목표는 점점 여기

는 피드 내 CDATA이다 CDATA 내의 <description> 태그에있는 가격.

그래서 각 <item> 노드는이처럼 보이는 그들을 포함

<a href="//www.ss.lv/msg/lv/real-estate/flats/riga/centre/colfo.html"> 
    <img align=right border=0 src="//i.ss.lv/images/2014-08-25/346391/VHkPH0FiQVo=/1.t.jpg" width="160" height="120" alt=""> 
</a> 
Rajons: <b>centrs</b> 
<br/>Iela: <b>Rūpniecības 7</b><br/>Ist.: <b>4</b> 
<br/>m2: <b>145</b><br/>Sērija: <b>Renov.</b><br/>: <b>10.34 €</b> 
<br/>Cena: <b>1,500 €/mēn.</b><br/> 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // this one 
<br/><b><a href="//www.ss.lv/msg/lv/real-estate/flats/riga/centre/colfo.html">Apskatīt sludinājumu</a></b><br/><br/> 

그래서 목표는 가격 (시나)에 대한 XPath는 검색을 사용하여입니다. 마크 업에 따르면 일반 텍스트 노드 (요소가 아니거나 태그가 아닙니다)입니다.

그래서 우리는 "시나"포함하는 텍스트 요소 대상 : 그래서

//text()[contains(., "Cena")] 

는 다음 형제에게 특정 값을 포함 <b> 태그가 각시나/가격, 그래서 우리는 각시나/가격 및 지점을 대상으로 다음 형제는 <b> 태그

item(0)->nextSibling->nodeValue 
Cena/Price -> nextSibling (which is b tag) -> its value 
+0

감사합니다. 귀하의 솔루션을 통해 나는 필요한 모든 값을 얻을 수 있지만 코드를 조금 설명 할 수 있습니다. 그래서 단순히 붙여 넣기 만 복사하지는 않습니다. 우리가 xpath를 사용했지만 이전 질문에서 DOM 파싱을 사용하는 이유는 무엇입니까? – Santar

+0

hello @Santar 설명을 추가했습니다. – Ghost