Php 获取特定节点中xml文件中段落标记内的文本

Php 获取特定节点中xml文件中段落标记内的文本,php,xml-parsing,simplexml,Php,Xml Parsing,Simplexml,我有这个xml文件 http://www.metacafe.com/tags/cats/rss.xml 使用此代码: $xml = simplexml_load_file('http://www.metacafe.com/tags/cats/rss.xml', 'SimpleXMLElement', LIBXML_NOCDATA); echo $xml->channel->item->title . "<br>"; echo $xml->channel-&g

我有这个xml文件

http://www.metacafe.com/tags/cats/rss.xml
使用此代码:

$xml = simplexml_load_file('http://www.metacafe.com/tags/cats/rss.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo $xml->channel->item->title . "<br>";
echo $xml->channel->item->description . "<br>";

我不知道怎样才能得到这个结果

获取描述中的元素的原因是CDATA部分。对于XML解析器,CDATA会话的内容始终是文本。像
这样的元素不会读入DOM结构

一个简单的
strip_tags()
将删除所有元素。要获得更多控制,您需要将html片段加载到DOM中:

$html = <<<'HTML'
<a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/"><img src="http://s3.mcstatic.com/thumb/11150410/28824820/4/directors_cut/0/1/dad_challenges_kids_to_climb_walls_to_get_candy.jpg?v=1" align="right" border="0" alt="Dad Challenges Kids to Climb Walls to Get Candy" vspace="4" hspace="4" width="134" height="78" /></a>
                <p>
                Nick Dietz compiles some of the week's best viral videos, 
                including an elephant trying really hard to break a stick, a cat
                sunbathing and kids climbing up the walls to get candy. Plus, 
                making  music with a Ford Fiesta.                              
                <br>Ranked <strong>4.00</strong> / 5 | 78 views | <a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/">0 comments</a><br/>
                </p>
                <p>
                 <a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/"><strong>Click here to watch the video</strong></a> (02:38)<br/>
                    Submitted By:                       <a href="http://www.metacafe.com/channels/CBS/">CBS</a><br/>
                    Tags:
                    <a href="http://www.metacafe.com/topics/penna/">Penna</a>&nbsp;                 <br/>
                    Categories: <a href='http://www.metacafe.com/videos/entertainment/'>Entertainment</a>
               </p>

        <br>
HTML;

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);

$content = $xpath->evaluate("string(//p[1]/text())");
var_dump($content);

$html=它是html。。。您已经在使用DOM操作获取xml节点。这是一个简单的扩展,可以在该节点中拆分html并只提取所需的位。您能给我举个例子吗?请注意,完全没有必要将
LIBXML\u NOCDATA
传递到SimpleXML;只要您请求元素的字符串内容,所有CDATA和文本节点都将被适当地展平。如果您正在执行的不是
echo
,强制变量成为字符串的语法是
(string)$var
,例如
$html=(string)$xml->channel->item->description
。编辑了一个添加的示例。请回答另一个问题:是否只获取有关标记的锚文本?我的意思是:佩纳,Bjbj,再见。谢谢你对我的项目的宝贵帮助$xpath->evaluate(“//a”);将返回DomeElement节点的DOMNodeList。您可以使用foreach()对其进行迭代,并读取$nodeValue属性。“转义的元素被转换(
返回到
)”-除非我有误解,否则这是错误的:CDATA完全按原样保留所有数据,在到达结尾之前,CDATA块中的转义将不起作用。
&
仍然是一个文本,因此您只需要从字面上理解,
]
请参见
Dad Challenges Kids to Climb Walls to Get Candy
Nick Dietz compiles some of the week's best viral videos, 
including an elephant trying really hard to break a stick, a cat
sunbathing and kids climbing up the walls to get candy. Plus, 
making  music with a Ford Fiesta.
$html = <<<'HTML'
<a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/"><img src="http://s3.mcstatic.com/thumb/11150410/28824820/4/directors_cut/0/1/dad_challenges_kids_to_climb_walls_to_get_candy.jpg?v=1" align="right" border="0" alt="Dad Challenges Kids to Climb Walls to Get Candy" vspace="4" hspace="4" width="134" height="78" /></a>
                <p>
                Nick Dietz compiles some of the week's best viral videos, 
                including an elephant trying really hard to break a stick, a cat
                sunbathing and kids climbing up the walls to get candy. Plus, 
                making  music with a Ford Fiesta.                              
                <br>Ranked <strong>4.00</strong> / 5 | 78 views | <a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/">0 comments</a><br/>
                </p>
                <p>
                 <a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/"><strong>Click here to watch the video</strong></a> (02:38)<br/>
                    Submitted By:                       <a href="http://www.metacafe.com/channels/CBS/">CBS</a><br/>
                    Tags:
                    <a href="http://www.metacafe.com/topics/penna/">Penna</a>&nbsp;                 <br/>
                    Categories: <a href='http://www.metacafe.com/videos/entertainment/'>Entertainment</a>
               </p>

        <br>
HTML;

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);

$content = $xpath->evaluate("string(//p[1]/text())");
var_dump($content);