foreach xml节点返回所选元素 [英] foreach xml node return selected element

查看:104
本文介绍了foreach xml节点返回所选元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能从我的xml cdata标签中获得选定的值?



到目前为止,在stackoverflow的帮助下,我可以获得所有的< b> 字符串中的标记

  $ 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> ,但我只想让我们说价格。
我用红色在stackoverflow中使用 - > item(x)来获得该值,但没有任何工作。如果我把它放在这个 echo $ b-> nodeValue->项目(2)。 < br>; echo $ b-> item(2) - > nodeValue。 <峰; br> 中; 。那么我应该在哪里放置它,或者我应该如何使用价格获得< b> 元素。价格总是在同一个地方。



以下是我在Feed中的CDATA:

 < a href =// www.ss.lv/msg/lv/real-estate/flats/riga/purvciems/deblb.html\"> 
< img align =rightborder =0src =// i.ss.lv/images/2014-10-01/349288/VHkAHkBlRlo=/1.t.jpgwidth = 160height =120alt =>
< / a>地区:< b>< b> Purvciems< / b>< / b>< br />
街道:< b>< b> Dudajeva g。 12< / b>< / b>< br />
房间:< b>< b> 2< / b>< / b>< br />
m2:< b>< b> 50< / b>< / b>< br />
类型:< b>< b> LT proj。< / b>< / b>< br />
:< b>< b> 3< / b> €< / b>< br />
价格:< 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ītsludinājumu< / a>< / b>< br />
< br />
]]>


解决方案

您可以尝试使用此方法解析这些价格:

  $ 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,
//我不知道这是翻译中的PRICE哈哈
$ price = $ price_tag-> item(0) - > nextSibling-> nodeValue;
$ temp ['price'] = $ price;
$ data [] = $ temp;
}

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

好的,为了解释:

因此,我们的目标是获取位于CDATA内的< description> 标签中的价格。

< item> 节点包含它们,它们看起来像这样:

 << ; 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.jpgwidth =160height = 120alt =>
< / a>
Rajons:< b> centrs< / b>
< br /> Iela:< b>Rūpniecības7< / b>< br /> Ist .:< b> 4< / b>
< b /> m2:< b> 145< / b>< b>Sērija:< b> Renov。< / b>< br>:< b> ; 10.34€< / b>
< br /> Cena:< b> 1,500€/mēn。< / b>< br />
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //这个
< br / >< b>< a href =// www.ss.lv/msg/lv/real-estate/flats/riga/centre/colfo.html\">Apskatītsludinājumu< / a>< / b> ;<峰; br /><峰; br />

所以我们的目标是通过使用xpath来搜索价格(Cena)。所以根据标记这是一个普通的文本节点(不是一个元素或不是一个标签)。

所以我们定位包含Cena的文本元素:

  // text()[contains(。,Cena)] 

因此,每个Cena / Price都有下一个包含该特定值的兄弟< b> 标签,所以我们定位每个Cena / Price并指向下一个< b> 标签

  item(0) - > nextSibling-> nodeValue 
Cena / Price - > nextSibling(这是b标签) - >其价值


How can I get only selected value from my xml cdata tag?

So far with help in stackoverflow I can get all the <b> tags in a string

$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>";
        }

but it will echo out all data which are inside <b>, but I want to get only let's say price. I red in stackoverflow to use ->item(x) to get that value, but nothing is working.If I put it like this echo $b->nodeValue->item(2) . "<br>"; or echo $b->item(2)->nodeValue . "<br>";. So where should I put it or what should I use to get only <b> element with price. The price will always be in the same place.

Here is my CDATA from feed:

<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 />
]]>

解决方案

You can try this method to parse those prices:

$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);

Okay, for the explanation:

So the goal is getting those prices which resides in the <description> tag inside the CDATA.

So each <item> node contains them which looks like this:

<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/>

So the goal is, by using xpath, search for the Prices (Cena). So according to markup this a normal text node (Not an element or is not a tag).

So we target that text element which contains "Cena":

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

So each Cena/Price it has the next sibling <b> tags which contain that particular value, so we target each Cena/Price and point the next sibling which is <b> tag

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

这篇关于foreach xml节点返回所选元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆