【问题标题】:Ignoring data when using SimpleXML to list children使用 SimpleXML 列出子项时忽略数据
【发布时间】:2020-10-07 05:08:19
【问题描述】:

我正在使用简单 XML 将选举结果数据格式化为用户友好的网页。我正在使用 PHP foreach 列出比赛和候选人,但是在每个被视为候选人的比赛下嵌套了一些额外的数据(准确地说是 7 个元素)。我怎样才能过滤掉它们?

这是 XML:https://ftpcontent.worldnow.com/wicu/BTI/election.xml

这是代码输出:https://lillydigitalmedia.com/election.php

代码如下:

        <?php
        $btiresults = simplexml_load_file("https://ftpcontent.worldnow.com/wicu/BTI/election.xml");
        echo "<p><em>Updated: " . $btiresults['Time'] . "</em></p>";
        foreach($btiresults->children() as $race) {
            echo "<h4 class='card-title' style='margin-top: 20px'><strong>" . $race->Name1 . "</strong></h4>";
            foreach($race->children() as $candidate) {
                echo "<p style='width: 100%;'><span style='float: left;'><strong>" . $candidate->Name . "</strong> <small>" . $candidate->Party . "</small></span><span style='float: right;'>" . $candidate->Votes . " votes</span></p>";
                echo "<div class='progress' style='height: 25px; width: 100%;'>";
                echo "<div class='progress-bar' role='progressbar' style='width:" . $candidate->PercentageOfVotesCast . "%' aria-valuenow='" . $candidate->PercentageOfVotesCast . "' aria-valuemin='0' aria-valuemax='100'>" . $candidate->PercentageOfVotesCast . "%</div>";
                echo "</div>";
            }
        }
    ?>

提前致谢。

【问题讨论】:

  • 到目前为止你尝试过什么?为什么不简单地检查它们并做点什么?
  • 您好,请问您能否在edit 问题中包含示例 XML 和输出在问题本身中。我们不仅不需要单击随机链接来帮助您,而且它们将来很有可能会停止工作,从而使这个问题对其他有同样问题的人毫无用处。

标签: php xml simplexml


【解决方案1】:

既然处理的是xml,不如使用xpath。

尝试以下类似的操作(在现有的echo "&lt;p&gt;&lt;em&gt;Updated: " . $btiresults['Time'] . "&lt;/em&gt;&lt;/p&gt;"; 之后):

$races = $btiresults->xpath('//Race');
        foreach($races as $race) {
            echo "<h4 class='card-title' style='margin-top: 20px'><strong>" . $race->Name1 . "</strong></h4>";
            foreach($race->xpath('./Candidate') as $candidate) {                
                echo "<p style='width: 100%;'><span style='float: left;'><strong>" . $candidate->Name . "</strong> <small>" . $candidate->Party . "</small></span><span style='float: right;'>" . $candidate->Votes . " votes</span></p>";
                echo "<div class='progress' style='height: 25px; width: 100%;'>";
                echo "<div class='progress-bar' role='progressbar' style='width:" . $candidate->PercentageOfVotesCast . "%' aria-valuenow='" . $candidate->PercentageOfVotesCast . "' aria-valuemin='0' aria-valuemax='100'>" . $candidate->PercentageOfVotesCast . "%</div>";                                
                echo "</div>";
            }
        }

看看它是否有效。

【讨论】:

  • 谢谢。这做到了。我会记住这一点,以应对未来的类似情况。
【解决方案2】:

您可以简单地查看元素的标签名称,使用 SimpleXMLElement::getName

将你的内部 foreach 循环的内容包装到一个 if 条件中,检查它是否是Candidate

foreach($race->children() as $candidate) {
  if($candidate->getName() == 'Candidate') {
    echo '…';
  }
}

(将变量从$candiate 重命名为$child 可能是有意义的,因为在foreach 循环中为它分配一个值时,您不知道它是否实际上是一个Candidate 节点然而……但这是一个相当哲学的方面。)

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2012-12-30
    • 2011-01-23
    • 2023-03-22
    相关资源
    最近更新 更多