用PHP解析XML CDATA [英] Parsing XML CDATA with PHP

查看:103
本文介绍了用PHP解析XML CDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我不知道该怎么解决. 我有一个XML文件(实际上是RSS),我正在尝试用PHP进行解析,但是CDATA标记为空白.

I have a little problem that I can't figure out how to solve. I have an XML (actually it's RSS) file that I'm trying to parse with PHP, but the CDATA tag come out blank.

这是 XML 代码 这是 PHP文件

一切正常,除了没有打印描述标签. 如果有人可以帮助我,我将非常感激.

Everything works fine, except that the description tag is not printing. I would be very grateful if some one could help.

推荐答案

只是出于好奇,在获取您的XML 之后(我希望我不会在此过程中销毁它-我会看看是否可以编辑OP进行更正):

Just out of curiosity, after getting your XML (I hope I didnt't destroy it in the process -- I'll see if I can edit the OP to correct it) :

  • 您是否将说明强制转换为字符串?


我的意思是您可以使用此:


What I mean is you could use this :

$xml = simplexml_load_string($str);
foreach ($xml->channel->item as $item) {
    var_dump($item->description);
}

但这只会让您知道:

object(SimpleXMLElement)[5]
object(SimpleXMLElement)[3]

那不是很好...


您需要像这样将数据转换为字符串:


You need to cast the data to string, like this :

$xml = simplexml_load_string($str);
foreach ($xml->channel->item as $item) {
    var_dump((string)$item->description);
}

您将获得描述:

string '

This is one of the content that I need printed on the screen, but nothing is happening. Please, please...output something... <br /><br /> <b>Showing</b>: 2 weeks<br /> <b>Starting On</b>: August 7, 2009 <br /> <b>Posted On</b>: August 7, 2009 <br />
<a href="http://www.mysite.com">click to view</a> 
            ' (length=329)

string '

Another content...This is another of the content that I need printed on the screen, but nothing is happening. Please, please...output something... <br /><br /> <b>Showing</b>: 2 weeks<br /> Starting On: August 7, 2009 <br /> <b>Posted On</b>: August 7, 2009
; 
               ' (length=303)

(如果在XML缩进的情况下,在其中使用trim可能会很有用)

(Using trim on those might prove useful, btw, if you XML is indented)


否则,我们可能需要您的php代码(至少,这对于了解您如何获得description标记;-)很有用)


Else... Well, we'll probably need your php code (at least, would be useful to know how you are getting to the description tag ;-) )

编辑

感谢重新格式化的XML!

Thanks for the reformated XML !

如果我去pastebin,则在页面底部的文本区域中,在XML的开头,在<?xml version="1.0" encoding="utf-8"?>

If I go to pastebin, in the textarea at the bottom of the page, there is a white space at the beginning of the XML, before the <?xml version="1.0" encoding="utf-8"?>

如果您的真实XML数据中有一个,它将成为问题的根源:它不是有效的XM1(XML声明必须是XML数据中的 first 东西).
您会收到类似这样的错误:

If you have that one in your real XML data, it will be a source of problem : it is not valid XMl (the XML declaration has to be the first thing in the XML data).
You'll get errors like this one :

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : XML declaration allowed only at the start of the document

你能检查一下吗?
如果问题出在这里,则应激活 error_reporting display_errors ;-)帮助!

Can you check that ?
And, if the problem is here, you should activate error_reporting and display_errors ;-) That would help !

在查看PHP文件后进行

EDIT after taking a look at the PHP file :

在for循环中,您正在执行此操作以获取描述数据:

In your for loop, you are doing this to get your description data :

$item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

我说

description不包含任何childNode;直接使用它的nodeValue怎么样?
像这样:

description doesn't contain any childNode, I'd say ; what about using it's nodeValue directly ?
Like this :

$item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->nodeValue;

这种方式似乎效果更好:-)

It seems to be working better this way :-)

作为附带说明,我想您可以对其他标签执行相同的操作;例如,这似乎也可行:

As a sidenote, you could probably do the same for other tags, I suppose ; for instance, this seems to be working too :

$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')->item(0)->nodeValue;

这给你什么?

另一个这是我可能会使用的代码:

Another EDIT : and here is the code I would probably use :

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($str);         // I changed that because I have the XML data in a string

//get elements from "<channel>"
$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')->item(0)->nodeValue;

//output elements from "<channel>"
echo "<p><a href='" . $channel_link . "'>" . $channel_title . "</a>";
echo "<br />";
echo $channel_desc . "</p>";

//get and output "<item>" elements
$x = $xmlDoc->getElementsByTagName('item');
for ($i=0 ; $i<=1 ; $i++) {
    $item_title = $x->item($i)->getElementsByTagName('title')->item(0)->nodeValue;
    $item_link = $x->item($i)->getElementsByTagName('link')->item(0)->nodeValue;
    $item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->nodeValue;
    echo ("<p><a href='" . $item_link
    . "'>" . $item_title . "</a>");
    echo ("<br />");
    echo ($item_desc . "</p>");
    echo' <p />';
}

请注意,我将XML数据存储在字符串中,不需要从URL提取数据,因此我使用的是loadXML方法,而不是load.

Note I have the XML data in a string, and I don't need to fetch it from an URL, so I'm using the loadXML method and not load.

主要区别在于,我删除了一些childNodes访问权限,但我认为这是不必要的.
这对您来说好吗?

The major difference is that I removed some childNodes accesses, that I feel were not necessary.
Does this seem OK to you ?

这篇关于用PHP解析XML CDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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