当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - Add a 'break' in my php code

问题描述:

Recently I am using php to deal with a xml file.

But I don't know how to add a 'break' in my code.

The link is : http://babystory.diandian.com/rss

I want to use php to get the article named '[x]快乐王子'.But "foreach()" give me the last article named '[1]狼和七只小山羊'.

I want to add a break in "foreach()" so that I can get the article I want.

Here is my php code:

<?php

$xml = simplexml_load_file("http://babystory.diandian.com/rss");

foreach($xml->children() as $child){}

foreach($child->children() as $child2)

foreach($child2->children() as $child3)

{

$element = $child3->getName();

if($element=='title')

{

echo "<h1>" . $child3 . "</h1>";

}

if($element=='description')

{

echo $child3;

}

}

?>

Could you help me to edit my code.Thank you very much.

网友答案:

If you need to get the first item from your feed you don't need any foreach loops at all.

$xml = simplexml_load_file("http://babystory.diandian.com/rss");
$item = (array)$xml->channel->item[0]; // the article [x]快乐王子 array

Now $item is array of the first article information. So $item['title'] is [x]快乐王子, $item['link'] is http://babystory.diandian.com/post/2012-01-15/14770976, etc.

网友答案:

Try this inside your last foreach....

i.e. .....

foreach($child2->children() as $child3){
   if($child3->getName() == "title" && $child3 == "THE TITLE I WANT"){
       $saveItem = $child3;
       break 3;
   }
}
echo $saveItem;
分享给朋友:
您可能感兴趣的文章:
随机阅读: