使用 SimpleXML 列出子项时忽略数据
Ignoring data when using SimpleXML to list children
我正在使用 Simple 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>";
}
}
?>
提前致谢。
您可以简单地查看元素的标签名称,使用 SimpleXMLElement::getName
。
将内部 foreach 循环的内容包装到 if 条件中,检查它是否 Candidate
:
foreach($race->children() as $candidate) {
if($candidate->getName() == 'Candidate') {
echo '…';
}
}
(将变量从 $candiate
重命名为 $child
可能有意义,因为在 foreach 循环中为它赋值时,您不知道它是否实际上是一个 Candidate
节点......但这是一个相当哲学的方面。)
既然你是处理xml,那你还不如用xpath。
尝试如下操作(在现有 echo "<p><em>Updated: " . $btiresults['Time'] . "</em></p>";
之后):
$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>";
}
}
看看它是否有效。
我正在使用 Simple 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>";
}
}
?>
提前致谢。
您可以简单地查看元素的标签名称,使用 SimpleXMLElement::getName
。
将内部 foreach 循环的内容包装到 if 条件中,检查它是否 Candidate
:
foreach($race->children() as $candidate) {
if($candidate->getName() == 'Candidate') {
echo '…';
}
}
(将变量从 $candiate
重命名为 $child
可能有意义,因为在 foreach 循环中为它赋值时,您不知道它是否实际上是一个 Candidate
节点......但这是一个相当哲学的方面。)
既然你是处理xml,那你还不如用xpath。
尝试如下操作(在现有 echo "<p><em>Updated: " . $btiresults['Time'] . "</em></p>";
之后):
$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>";
}
}
看看它是否有效。