通过 php 将 XML 数据转换为 html

我正在尝试将 XML 中的内容转换为 HTML,以显示在浏览器上,我找到了在线转换器,这里是链接https://codebeautify.org/xml-to-html-converter它的作用是,基本上它获取XML数据并给出html输出,如下图所示

https://img1.sycdn.imooc.com/652b8eab0001ac9915750707.jpg

但我想在 PHP 中执行此操作,我在 google 中搜索过此内容,但没有找到相关的解决方案。


下面是我尝试过的


<?php


$file = 'https://demo.conitor.in/Remembered/views/feed.php';


if(!$xml = simplexml_load_file($file)){

    echo "file not found!";

} else {

    echo "<pre>";

    print_r($xml);

    echo "</pre>";    

}


?>

这是我的 xml 数据 URL:- https://demo.conitor.in/Remembered/views/feed.php


杨魅力
浏览 83回答 0
0回答

宝慕林4294392

simplexml_load_file会将给定文件中格式正确的 XML 文档转换为对象。您可以按如下方式迭代该对象。<?php$file = 'https://demo.conitor.in/Remembered/views/feed.php';if(!$xml = simplexml_load_file($file)){    // Will return false if the file doesn't contain a well formated XML document.    echo "file not found!";} else {    echo "<table><thead><tr><th>Number</th><th>Title</th><th>First Name</th><th>State</th></tr></thead><tbody>";    $i = 0;    // Refere your SimpleXmlObject Struture for more information.    foreach ($xml->channel->item as $item) {       echo "<tr><td>" .++$i ."</td><td>" . $item->title . "</td><td>" . $item->DeceasedFirstName . "</td><td>" . $item->State ."</td></tr>";    }    echo "</tbody></table>";}?>
打开App,查看更多内容
随时随地看视频慕课网APP