在php表中显示xml文件中的数据
我有一个Xml文件,我只想将该数据文件显示为html表格格式。下面是我的Xml代码在php表中显示xml文件中的数据,php,xml,Php,Xml,我有一个Xml文件,我只想将该数据文件显示为html表格格式。下面是我的Xml代码 <table> <thead> <tr> <th>Name</th> <th>Contact Number</th> <th>Address</th> <th>Email<
<table>
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th>Email</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
<?php
$xml = simplexml_load_file("banner.xml");
foreach ($xml->college->student as $ban) :
?>
<tr>
<td><?php echo $ban->Profile->Name; ?></td>
<td><?php echo $ban->Profile->ContactNumber; ?></td>
<td><?php echo $ban->Profile->Address->AddressLine1; ?></td>
<td><?php echo $ban->Profile->Email; ?></td>
<td><?php echo $ban->Profile->Picture; ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
My xml looks like this:
<college fullname="xCol">
<student knumber="000555">
<Profile>
<Name>testtt</Name>
<ContactNumber>061-123-1235</ContactNumber>
<Address>
<AddressLine1>testttaxxxx</AddressLine1>
<AddressLine2>testttsss</AddressLine2>
<City>testttaaaa</City>
<County>testttwwww</County>
</Address>
<Email>testtt@test</Email>
<Picture>testtt.jpg</Picture>
</Profile>
</student>
<student knumber="34333">
<Profile>
<Name>testtt</Name>
<ContactNumber>061-123-1235</ContactNumber>
<Address>
<AddressLine1>testttaxxxx</AddressLine1>
<AddressLine2>testttsss</AddressLine2>
<City>testttaaaa</City>
<County>testttwwww</County>
</Address>
<Email>testtt@test</Email>
<Picture>testtt.jpg</Picture>
</Profile>
</student>
名称
联系电话
住址
电子邮件
照片
我的xml如下所示:
测试
061-123-1235
testtaxxxx
testttsss
testttaaa
testttwww
testtt@test
testtt.jpg
测试
061-123-1235
testtaxxxx
testttsss
testttaaa
testttwww
testtt@test
testtt.jpg
我对xml相当陌生,这可能非常简单,但我无法理解我做错了什么。
我得到的输出只是标题:
这是我在我的网站上得到的输出:
姓名联系电话地址电子邮件照片
*编辑-显示第一个配置文件,但无法获取第二个,有什么想法吗?将代码更改为:
<?php
$xml = simplexml_load_file("banner.xml");
foreach ($xml->student as $ban):
?>
<tr>
<td><?php echo $ban->Profile->Name;?></td>
<td><?php echo $ban->Profile->ContactNumber;?></td>
<td><?php echo $ban->Profile->Address->AddressLine1;?></td>
<td><?php echo $ban->Profile->Email;?></td>
<td><?php echo $ban->Profile->Picture;?></td>
</tr>
<?php
endforeach;
?>
您的xml文件中没有$xml->banner
,但有许多student
,因此我将其更改为$xml->student
每个学生
都有一个档案
,并将其添加到表中
Address
有多个子项,我只在这段代码中添加了AddressLine1
,可能需要修改。将原始代码编辑为-
<?php
$xml = simplexml_load_file("banner.xml");
foreach ($xml->college->student as $ban) :
?>
<tr>
<td><?php echo $ban->Profile->Name; ?></td>
<td><?php echo $ban->Profile->ContactNumber; ?></td>
<td><?php echo $ban->Profile->Address->AddressLine1; ?></td>
<td><?php echo $ban->Profile->Email; ?></td>
<td><?php echo $ban->Profile->Picture; ?></td>
</tr>
<?php
endforeach;
?>
$ban
和$banner
。您需要将echo$banner->
替换为echo$ban->
(来自您的foreach)@kerbholz不起作用,但感谢您在foreach()中注意到另一件事:$xml->banner
在您的代码中没有更改为foreach($xml->student as$ban)