Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中显示此XML表_Php_Xml_Html Table - Fatal编程技术网

如何在PHP中显示此XML表

如何在PHP中显示此XML表,php,xml,html-table,Php,Xml,Html Table,我有这个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:1

我有这个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>  

下面是一个简单的教程,介绍如何使用PHP5的simpleXML函数在php中解析xml文件:
在这种情况下,代码可能是:

<?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表中。

最后接受了@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>




您将需要一个XML DOM解析器。或者您可以编写XSLT。没有“自动”或自动方式。在任何支持“查找和替换”功能(CTRL+H)的文本编辑器中打开此文件。这将是一种简单的方法。您需要创建一个名为file.xml的文件,该文件包含您在问题中提供的xml内容,位于您拥有php脚本的同一文件夹中。是的,我这样做了,这是我的xml,它和php脚本在同一个文件夹中,也被命名为file.xml(用于测试目的)。我需要用Html显示该xml的所有内容,你认为它可以用你的方式实现还是需要另一种方法?@RonEskinder这次你提供了一个结构稍有不同的文件。。这种方法仅在xml文件具有相同结构时有效,否则需要修改脚本以解析不同的xml文件结构。。。或者您可以使用xls样式表,它是xml文件的一种css:)请看这里: