【问题标题】:How can I show this XML table in PHP如何在 PHP 中显示这个 XML 表
【发布时间】:2017-07-28 03:28:04
【问题描述】:

我有这个带有表格的 XML,但我不知道如何在 PHP 中显示表格,html 无法将节点识别为表格。

我是否必须读取每个单独的节点并将其更改为

?还是有更自动化的方法来做到这一点?
<?xml version="1.0" encoding="utf-8"?>
<afpdb lang="fr-FR">
    <head>
        <message type="203" file="s4133-0000000-203-fr" timestamp="2014-04-16T12:16:26+02:00" />
    </head>
    <body>
        <competition id="866" label="Brésil 2014">
            <discipline code="FB" name="Football">
                <evt id="4133" label="Brésil 2014" gender="M" date="2014-06-12T00:00:00-03:00">
                    <country iso="BRA" code="BRA" name="Brésil" />
                    <phase id="2714" code="TP8FI" type="PH1PT">
                        <group id="9276" name="C" label="Groupe C">
                            <table type="TAFUL">
                                <thead>
                                    <tcol num="1" type="TCVAL" code="CCPOS" />
                                    <tcol num="2" type="TCXML" code="CCEQU" />
                                    <tcol num="3" type="TCVAL" code="CCPTS" />
                                    <tcol num="4" type="TCVAL" code="CCJOU" />
                                    <tcol num="5" type="TCVAL" code="CCGAG" />
                                    <tcol num="6" type="TCVAL" code="CCNUL" />
                                    <tcol num="7" type="TCVAL" code="CCPER" />
                                    <tcol num="8" type="TCVAL" code="CC_BP" />
                                    <tcol num="9" type="TCVAL" code="CC_BC" />
                                    <tcol num="10" type="TCVAL" code="CCDIF" />
                                </thead>
                                <trow>
                                    <tcol num="1" val="1" />
                                    <tcol num="2">
                                        <team id="2090" type="CENAT" display="Brésil">
                                            <country iso="BRA" code="BRA" name="Brésil" />
                                        </team>
                                    </tcol>
                                    <tcol num="3" val="0" />
                                    <tcol num="4" val="0" />
                                    <tcol num="5" val="0" />
                                    <tcol num="6" val="0" />
                                    <tcol num="7" val="0" />
                                    <tcol num="8" val="0" />
                                    <tcol num="9" val="0" />
                                    <tcol num="10" val="0" />
                                </trow>
                                <trow>
                                    <tcol num="1" val="1" />
                                    <tcol num="2">
                                        <team id="2103" type="CENAT" display="Cameroun">
                                            <country iso="CMR" code="CMR" name="Cameroun" />
                                        </team>
                                    </tcol>
                                    <tcol num="3" val="0" />
                                    <tcol num="4" val="0" />
                                    <tcol num="5" val="0" />
                                    <tcol num="6" val="0" />
                                    <tcol num="7" val="0" />
                                    <tcol num="8" val="0" />
                                    <tcol num="9" val="0" />
                                    <tcol num="10" val="0" />
                                </trow>
                                <trow>
                                    <tcol num="1" val="1" />
                                    <tcol num="2">
                                        <team id="2107" type="CENAT" display="Croatie">
                                            <country iso="HRV" code="CRO" name="Croatie" />
                                        </team>
                                    </tcol>
                                    <tcol num="3" val="0" />
                                    <tcol num="4" val="0" />
                                    <tcol num="5" val="0" />
                                    <tcol num="6" val="0" />
                                    <tcol num="7" val="0" />
                                    <tcol num="8" val="0" />
                                    <tcol num="9" val="0" />
                                    <tcol num="10" val="0" />
                                </trow>
                                <trow>
                                    <tcol num="1" val="1" />
                                    <tcol num="2">
                                        <team id="2175" type="CENAT" display="Mexique">
                                            <country iso="MEX" code="MEX" name="Mexique" />
                                        </team>
                                    </tcol>
                                    <tcol num="3" val="0" />
                                    <tcol num="4" val="0" />
                                    <tcol num="5" val="0" />
                                    <tcol num="6" val="0" />
                                    <tcol num="7" val="0" />
                                    <tcol num="8" val="0" />
                                    <tcol num="9" val="0" />
                                    <tcol num="10" val="0" />
                                </trow>
                            </table>
                        </group>
                    </phase>
                </evt>
            </discipline>
        </competition>
    </body>
</afpdb>  

【问题讨论】:

  • 您将需要一个 XML DOM 解析器。或者您可以编写一个 XSLT。没有“自动化”,或者说是自动化的方式。
  • 在任何支持“查找和替换”功能 (CTRL+H) 的文本编辑器中打开此文件。这是一种简单的方法。

标签: php xml html-table


【解决方案1】:

这是一个简单的教程,向您展示如何使用 PHP5 的 simpleXML 函数在 php 中解析 xml 文件: http://blog.teamtreehouse.com/how-to-parse-xml-with-php5
在这种情况下,代码可能是这样的:

<?php
$data = simplexml_load_file('file.xml');
$source = $data->body->competition->discipline->evt->phase->group;
foreach ($source->table as $tableElement) {
echo "<h4>THEAD</h4>";
foreach ($tableElement->thead[0] as $tcol) {
    echo "Num attribute: ".$tcol->attributes()->num."<br />";
    echo "Type attribute: ".$tcol->attributes()->type."<br />";
    echo "Code attribute: ".$tcol->attributes()->code."<br />";
    echo "####<br/>";
}
for ($i=0;$i<count($tableElement->trow);$i++){
    echo "<h4>TROW ".$i."</h4>";
    foreach ($tableElement->trow[$i] as $tcol) {
        echo "Num attribute: ".$tcol->attributes()->num."<br />";
        echo "Val attribute: ".$tcol->attributes()->val."<br />";

        echo "####<br/>";
    }
}


}
?>

然后您可以修改代码以显示在一个简单的 html 表格中..

【讨论】:

  • 您需要创建一个名为 file.xml 的文件,其中包含您在问题中提供的 xml 内容,在您拥有 php 脚本的同一文件夹中。
  • 是的,这是我的 XML,它与 php 脚本位于同一文件夹中,也名为 file.xml(用于测试目的)pastebin.com/mc6CA69y
  • 我需要在 Html 中显示该 XML 的所有内容,您认为它可以通过您的方式实现还是需要其他方法?
  • @RonEskinder 这次您提供了一个结构稍有不同的文件。这种方法仅适用于 xml 文件具有相同结构的情况,否则您需要修改脚本以解析不同的 xml文件结构...或者您可以使用 xls 样式表,它是一种用于 xml 文件的 css :) 看看这里:w3schools.com/xsl
【解决方案2】:

最终采纳了@Cristian 的建议,为 XML 制作一个 XLS 模板,然后在 php 中处理,这是结果

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<xsl:template match="/">
  <html>
  <body>

  <!-- Groups --> 
  <xsl:for-each select="afpdb/body/competition/discipline/evt/phase/group">
  <xsl:sort select="match/@id"/>

  <div class="row-fluid">
      <div class="span6">
        <div class="widget-block">
          <div class="widget-head">
            <h5><i class="color-icons soccer_co"></i>  <xsl:value-of select="@label"/></h5>
            </div>
                <div class="widget-content">
                <div class="widget-box">
                <table class="table user-tbl">
                        <xsl:for-each select="match">
                        <xsl:variable name="flag1" select="res[1]/team/country/@iso" />
                        <xsl:variable name="flag2" select="res[2]/team/country/@iso" />
                        <xsl:variable name="dt" select="@timestamp"/>
                        <xsl:variable name="link_id" select="@id"/>
                          <tr>
                            <td class="center"><xsl:value-of select="concat(substring($dt, 9, 2), '/', substring($dt, 6, 2), '/', substring($dt, 1, 4), ' - ', substring($dt, 12, 5))" /><br/><strong><xsl:value-of select="@dow"/></strong></td>
                            <td class="center"><img width="35px" src="{concat('http://vivo.futebolivr.com/LIB/comentarios/flags/',$flag1,'.png')}"  /><br/><h4><xsl:value-of select="res[1]/team/@display"/></h4></td>
                            <td class="center"><xsl:value-of select="datas/stadium/@name"/><span class="user-position"><xsl:value-of select="datas/stadium/city/@name"/></span></td>
                            <td class="center"><img width="35px" src="{concat('http://vivo.futebolivr.com/LIB/comentarios/flags/',$flag2,'.png')}"  /><h4><xsl:value-of select="res[2]/team/@display"/></h4></td>
                            <td class="center"><a class="iframe btn btn-info cboxElement" href="http://vivo.futebolivr.com/LIB/comentarios/comment_live2.php?id={$link_id}">Comentarios</a></td>
                          </tr>
                      </xsl:for-each>
                    </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    </xsl:for-each>

  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    相关资源
    最近更新 更多