猿问

从 XML 文件读取数据并打印选定的数据

我有一个 XML 文件中的博客提要:


<item>

    <title>Title</title>

    <link>link</link>

    <pubDate>Date</pubDate>

</item>

我正在尝试获取并回显上述信息,但它不起作用。这是我的方法:


<?php foreach ($results as $index => $record) : ?>

    <div class="col-12">

        <?php 

            echo("

                <a class='item'>

                    <div class='item__card'>

                        $blogAuthor ->item ->link;

                        $blogTitle ->item ->title;

                        $blogDate ->item ->pubDate;

                    </div>

                </a>"

            );

        ?>

    </div>

<?php endforeach; ?>

我哪里错了?


慕容3067478
浏览 130回答 2
2回答

万千封印

使用你缺少的 php simplexml_load_string这是你的代码<?php$strxml='<?xml version="1.0" encoding="UTF-8"?><rss>&nbsp; &nbsp; <item>&nbsp; &nbsp; &nbsp; &nbsp; <title>Title 1</title>&nbsp; &nbsp; &nbsp; &nbsp; <link>link 1</link>&nbsp; &nbsp; &nbsp; &nbsp; <pubDate>Date 1</pubDate>&nbsp; &nbsp; </item>&nbsp; &nbsp; <item>&nbsp; &nbsp; &nbsp; &nbsp; <title>Title 2</title>&nbsp; &nbsp; &nbsp; &nbsp; <link>link 2</link>&nbsp; &nbsp; &nbsp; &nbsp; <pubDate>Date 2</pubDate>&nbsp; &nbsp; </item></rss>';$col = simplexml_load_string($strxml);&nbsp;echo '<pre>';print_r($col);foreach( $col as $item ){&nbsp; &nbsp; $title=$item->title;&nbsp; &nbsp; echo $title.'<br />';}输出 :标题 1 标题 2
随时随地看视频慕课网APP
我要回答