Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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 - Fatal编程技术网

在php表中显示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<

我有一个Xml文件,我只想将该数据文件显示为html表格格式。下面是我的Xml代码

     <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)