使用PHP将XML转换为HTML表

使用PHP将XML转换为HTML表,php,html,xml,yii,Php,Html,Xml,Yii,我正在尝试使用php将下面的XML提要转换为HTML表 <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0"> <channel> <title> Latest consumer reviews on abc.com from 28/09/2015 </titl

我正在尝试使用php将下面的XML提要转换为HTML表

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0">
<channel>
<title>
Latest consumer reviews on abc.com from 28/09/2015
</title>
<link>https://www.example.com</link>
<description>
<![CDATA[ bla bla bla bla. ]]>
</description>
<item>
<title>2015 AUDI A1 SPORTBACK TFSI</title>
<link>
http://localhost/frontend/www/audi/a1/2015/
</link>
<pubDate>1443607871</pubDate>
<enclosure length="" type="image/jpeg"/>
<guid isPermaLink="true">
http://localhost/frontend/www/audi/a1/2015/
</guid>
<date>30/09/2015</date>
<time>10:11</time>
<id>1</id>
<image>
https://example.com/audi.jpg
</image>
</item>
<item>
<title>2015 BMW 3series</title>
<link>
http://localhost/frontend/www/bmw/3/2015/
</link>
<pubDate>1444117968</pubDate>
<enclosure length="" type="image/jpeg"/>
<guid isPermaLink="true">
http://localhost/frontend/www/bmw/3/2015/
</guid>
<date>06/10/2015</date>
<time>07:52</time>
<id>2</id>
<image>
https://example.com/bmw.jpg
</image>
</item>
</channel>
</rss>
但是当我打印时,我输出的不是完整的
数组。我得到的只是这个

SimpleXMLElement Object ( [title] => Latest consumer reviews on example.com from 28/10/2015 [link] => https://www.example.com [description] => SimpleXMLElement Object ( ) ) 

没有包含HTML部分,因为我甚至不能让循环正常工作。知道我错过了什么或做错了什么吗?谢谢

尝试改用XMLtoArray库()

它将在多维数组中转换您的xml,然后在数组中循环将是一项简单的工作

您更新的代码应该如下所示

$baseurl = 'http://localhost/frontend/www/';
$url = $baseurl.'comment/rss/comments/?t='.time().'&l=4';
$xml = file_get_content($url,true);
$array = XML2Array::createArray($xml);

print_r($array);

您还可以使用XSLT将XML转换为HTML

$baseurl = 'http://localhost/frontend/www/';
$url = $baseurl.'comment/rss/comments/?t='.time().'&l=4';

$xml = new DOMDocument();
$xml->loadXML(file_get_contents($url));

$xsl = new DOMDocument();
$xsl->load('feed-to-html-table.xsl');

$xsltp = new XSLTProcessor();
$xsltp->importStyleSheet($xsl);
echo $xsltp->transformToXML($xml);
html table.xsl的
提要可以是这样的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/rss/channel">
    <table>
      <xsl:apply-templates select="item" />
    </table>
  </xsl:template>
  <xsl:template match="item">
    <tr>
      <td><a href="{link}"><xsl:value-of select="title" /></a></td>
      <td><img src="{image}" /></td>
      <td><xsl:value-of select="date" /> <xsl:value-of select="time" /></td>
    </tr>
  </xsl:template>
</xsl:stylesheet>


现在它为我提供了从2015年10月28日开始在example.com上发布的
Array([channel]=>Array([title]=>最新消费者评论[link]=>https://www.example.com [description]=>Array([@cdata]=>blablablabla.))[@attributes]=>Array([version]=>2.0))
我的foreach循环就是这个tho
foreach($x作为数组){print\r($x)}
Yes如果正确,请尝试打印$array,您将了解获取此数组的原因要访问描述,请尝试
echo$x[“channel”][“description”][“@cdata”]
但是
在哪里?它不显示在数组中。为什么?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/rss/channel">
    <table>
      <xsl:apply-templates select="item" />
    </table>
  </xsl:template>
  <xsl:template match="item">
    <tr>
      <td><a href="{link}"><xsl:value-of select="title" /></a></td>
      <td><img src="{image}" /></td>
      <td><xsl:value-of select="date" /> <xsl:value-of select="time" /></td>
    </tr>
  </xsl:template>
</xsl:stylesheet>