Pas*_*TIN 17
只是出于好奇,在获得你的XML之后(我希望我在这个过程中没有破坏它 - 我会看看我是否可以编辑OP来纠正它):
我的意思是你可以使用这个:
$xml = simplexml_load_string($str);
foreach ($xml->channel->item as $item) {
var_dump($item->description);
}
Run Code Online (Sandbox Code Playgroud)
但它只会让你:
object(SimpleXMLElement)[5]
object(SimpleXMLElement)[3]
Run Code Online (Sandbox Code Playgroud)
哪个不太好......
您需要将数据转换为字符串,如下所示:
$xml = simplexml_load_string($str);
foreach ($xml->channel->item as $item) {
var_dump((string)$item->description);
}
Run Code Online (Sandbox Code Playgroud)
你得到的描述:
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)
Run Code Online (Sandbox Code Playgroud)
(使用trim那些可能证明有用,顺便说一下,如果你缩进XML)
否则......好吧,我们可能需要你的PHP代码(至少,知道你如何获得description标签会很有用;-))
编辑
感谢重新格式化的XML!
如果我转到pastebin,在页面底部的textarea中,在XML的开头有一个空格,之前是 <?xml version="1.0" encoding="utf-8"?>
如果你的真实XML数据中有那个,它将成为问题的根源:它是无效的XMl(XML声明必须是XML数据中的第一件事).
你会得到像这样的错误:
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : XML declaration allowed only at the start of the document
Run Code Online (Sandbox Code Playgroud)
你能检查一下吗?
并且,如果问题在这里,你应该激活error_reporting和display_errors;-)这将有所帮助!
在看了PHP文件之后编辑:
在for循环中,您这样做是为了获取您的描述数据:
$item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
Run Code Online (Sandbox Code Playgroud)
说明不包含任何childNode,我会说; 怎么样直接使用它的nodeValue?
像这样 :
$item_desc = $x->item($i)->getElementsByTagName('description')->item(0)->nodeValue;
Run Code Online (Sandbox Code Playgroud)
它似乎以这种方式更好地工作:-)
作为旁注,我想可能对其他标签做同样的事情; 例如,这似乎也有效:
$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')->item(0)->nodeValue;
Run Code Online (Sandbox Code Playgroud)
这给你带来了什么?
另一个编辑:这是我可能会使用的代码:
$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 />';
}
Run Code Online (Sandbox Code Playgroud)
注意我在字符串中有XML数据,我不需要从URL中获取它,所以我使用的是loadXML方法而不是load.
主要的区别是我删除了一些childNodes访问,我认为没有必要.
这对你好吗?