我有一个带有表格的XML,我不知道如何在PHP中显示表格,html不会将节点识别为表格。
我是否必须阅读每个节点并将其更改为< TR> < TD&GT ;?或者有更自动化的方法来做到这一点?
<?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>
答案 0 :(得分: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表中显示的代码..
答案 1 :(得分:-1)
最终采取了@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>