PHP SimpleXML XPath - 显示数据有问题

发布于 2024-11-19 11:24:18 字数 3586 浏览 2 评论 0原文

(从原来的问题移开。)

我在本地主机上有一个广播电台站点,这些页面是一系列包含和要求的页面 - 它们运行良好。

然而,在一页(即时间表一页)上,我在 DIV 中显示从 SimpleXML 文档中提取的一些数据。

这是 PHP 文档的编码:

        <div class="itemheading"><h1><span>Programme Guide</span></h1></div>
        <div class="itembody">
          <div id="progdays">
                        <ul>
<li><a href="sunsch.php" title="Sunday">Sunday</a></li>
<li class="currentday"><a href="monsch.php">Monday</a></li>
<li><a href="tuesch.php" title="Tuesday">Tuesday</a></li>
<li><a href="wedsch.php" title="Wednesday">Wednesday</a></li>
<li><a href="thursch.php" title="Thursday">Thursday</a></li>
<li><a href="frisch.php" title="Friday">Friday</a></li>
<li><a href="satsch.php" title="Saturday">Saturday</a></li>                                             </ul>
          </div>
<?php
require("monsch.php");
?>
                                                            </div>

这是 monsch.php 的内容:

        <?php

// set name of XML file
// normally this would come through GET
// it's hard-wired here for simplicity
$file = "monsched.xml";

// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
?> 

                            <dl class="standard">
                <dt><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><?php echo $xml->showtime; ?> - <?php echo $xml->show; ?></a></dt>
                <dd class="itemimg"><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><img src="<?php echo $xml->image; ?>" width="100" height="75" alt="<?php echo $xml->show; ?>" title="<?php echo $xml->show; ?>" /></a></dd>
                <dd class="itemdesc">
<?php echo $xml->showinfo; ?>
                </dd>
                <dd class="itemlink">
                  <a href="<?php echo $xml->link; ?>" title="<?php echo $xml->moreinfo; ?>"><span></span><?php echo $xml->moreinfo; ?></a>
                </dd>
              </dl>

使用 PHP 的 echo() 函数可以正常工作,但是我如何让 XML 文件显示其中的多个记录。

以下是 monsched.xml 的 XML 内容:

<?xml version="1.0"?>

演示者.php 约翰尼·多伊 约翰尼.jpg 00:00 约翰·多伊整夜 有关演出的更多信息

我按照 http://devzone.zend.com/article/651 (Baz最后的鲁尔曼例子就是我试图模仿的)。一切正常,除了一个问题......我如何使用 SimpleXML 从 XML 文件中的多个记录中读取 PHP 文件。

如果我有很多节目,我该如何从 XML 文件中提取所有节目,如上面的格式。

这些是在 XML 中包含描述和图像的节目: 这是我的 XML 格式:

<?xml version="1.0"?>
<link>presenter1.php</link>
<show>John Doe</show>
<image>overnight.jpg</image>
<showtime>01:00</showtime>
<showinfo>John Doe plays the hits
</showinfo>
<link>presenter2.php</link>
<show>Breakfast Show</show>
<image>headphones.jpg</image>
<showtime>06:00</showtime>
<showinfo>More music breakfast
</showinfo>

感谢所有帮助,我对 SimpleXML 和 XPath 相当陌生,但尝试让 Baz Luhrmann 示例适用于多个条目是很棘手的 - 有人能弄清楚吗?

该教程在其他方面很有帮助,但并没有回答我所有的问题。

(Moved from original question.)

I have a radio station site on localhost, and the pages are a series of include and requires - they work well.

However, on one page, a schedule one, I'm displaying within a DIV some data extracted from a SimpleXML document.

This is the PHP document's coding:

        <div class="itemheading"><h1><span>Programme Guide</span></h1></div>
        <div class="itembody">
          <div id="progdays">
                        <ul>
<li><a href="sunsch.php" title="Sunday">Sunday</a></li>
<li class="currentday"><a href="monsch.php">Monday</a></li>
<li><a href="tuesch.php" title="Tuesday">Tuesday</a></li>
<li><a href="wedsch.php" title="Wednesday">Wednesday</a></li>
<li><a href="thursch.php" title="Thursday">Thursday</a></li>
<li><a href="frisch.php" title="Friday">Friday</a></li>
<li><a href="satsch.php" title="Saturday">Saturday</a></li>                                             </ul>
          </div>
<?php
require("monsch.php");
?>
                                                            </div>

Here is the content of monsch.php:

        <?php

// set name of XML file
// normally this would come through GET
// it's hard-wired here for simplicity
$file = "monsched.xml";

// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
?> 

                            <dl class="standard">
                <dt><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><?php echo $xml->showtime; ?> - <?php echo $xml->show; ?></a></dt>
                <dd class="itemimg"><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><img src="<?php echo $xml->image; ?>" width="100" height="75" alt="<?php echo $xml->show; ?>" title="<?php echo $xml->show; ?>" /></a></dd>
                <dd class="itemdesc">
<?php echo $xml->showinfo; ?>
                </dd>
                <dd class="itemlink">
                  <a href="<?php echo $xml->link; ?>" title="<?php echo $xml->moreinfo; ?>"><span></span><?php echo $xml->moreinfo; ?></a>
                </dd>
              </dl>

It works well correctly using the echo() function of PHP, but how would I get the XML file to display more than one record from it.

Here's the XML content of monsched.xml:

<?xml version="1.0"?>

presenter.php
Johnny Doe
johnny.jpg
00:00

John Doe through the night

More about the show

I followed the tutorial at http://devzone.zend.com/article/651 (the Baz Luhrmann example at the end was what I was trying to emulate). and everything worked OK, except one problem.... how do I get the PHP file to, using SimpleXML, read from multiple records within the XML file.

If I have a lot of shows in it, how would I get it to extract all of them from the XML file like the format above.

These are the shows with descriptions and images within the XML:
This is my XML formatting:

<?xml version="1.0"?>
<link>presenter1.php</link>
<show>John Doe</show>
<image>overnight.jpg</image>
<showtime>01:00</showtime>
<showinfo>John Doe plays the hits
</showinfo>
<link>presenter2.php</link>
<show>Breakfast Show</show>
<image>headphones.jpg</image>
<showtime>06:00</showtime>
<showinfo>More music breakfast
</showinfo>

All help is appreciated with this, I'm fairly new to SimpleXML and XPath, but trying to get the Baz Luhrmann example to work for multiple entries is tricky - anyone able to figure it out?

The tutorial was otherwise helpful, though, but didn't answer all my questions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2024-11-26 11:24:18

您的情况的问题是您需要不同的 XML 结构:

<?xml version="1.0"?>
<podcasts>
  <podcast>
    <link>presenter1.php</link>
    <show>John Doe</show>
    <image>overnight.jpg</image>
    <showtime>01:00</showtime>
    <showinfo>John Doe plays the hits
    </showinfo>
  </podcast>
  <podcast>
    <link>presenter2.php</link>
    <show>Breakfast Show</show>
    <image>headphones.jpg</image>
    <showtime>06:00</showtime>
    <showinfo>More music breakfast
    </showinfo>
  </podcast>
</podcasts>

之后使用以下 PHP 代码:

$xml = simplexml_load_file($file)
foreach($xml->podcast as $row) {
  echo $row->link;
  ?>
  <dl class="standard">
            <dt><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>">
            <?php echo      $xml->showtime; ?> - <?php echo $xml->show; ?></a></dt>
            <dd class="itemimg"><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><img src="<?php echo $xml->image; ?>" width="100" height="75" alt="<?php echo $xml->show; ?>" title="<?php echo $xml->show; ?>" /></a></dd>
            <dd class="itemdesc">
               <?php echo $xml->showinfo; ?>
            </dd>
            <dd class="itemlink">
              <a href="<?php echo $xml->link; ?>" title="<?php echo $xml->moreinfo; ?>"><span></span><?php echo $xml->moreinfo; ?></a>
            </dd>
          </dl>

  <?php 
}

The problem in your case is taht you need diferent XML structure:

<?xml version="1.0"?>
<podcasts>
  <podcast>
    <link>presenter1.php</link>
    <show>John Doe</show>
    <image>overnight.jpg</image>
    <showtime>01:00</showtime>
    <showinfo>John Doe plays the hits
    </showinfo>
  </podcast>
  <podcast>
    <link>presenter2.php</link>
    <show>Breakfast Show</show>
    <image>headphones.jpg</image>
    <showtime>06:00</showtime>
    <showinfo>More music breakfast
    </showinfo>
  </podcast>
</podcasts>

After that use the following PHP code:

$xml = simplexml_load_file($file)
foreach($xml->podcast as $row) {
  echo $row->link;
  ?>
  <dl class="standard">
            <dt><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>">
            <?php echo      $xml->showtime; ?> - <?php echo $xml->show; ?></a></dt>
            <dd class="itemimg"><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><img src="<?php echo $xml->image; ?>" width="100" height="75" alt="<?php echo $xml->show; ?>" title="<?php echo $xml->show; ?>" /></a></dd>
            <dd class="itemdesc">
               <?php echo $xml->showinfo; ?>
            </dd>
            <dd class="itemlink">
              <a href="<?php echo $xml->link; ?>" title="<?php echo $xml->moreinfo; ?>"><span></span><?php echo $xml->moreinfo; ?></a>
            </dd>
          </dl>

  <?php 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文