在表php中显示来自xml文件的数据

时间:2018-02-21 11:24:06

标签: php xml

我有一个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相当新,这可能很简单,但我无法理解我做错了什么。 我得到的输出只是标题:

这是我在网站上获得的输出: 姓名联系电话地址电子邮件照片

*编辑 - 显示第一个人资料但无法获得第二个,任何想法?

2 个答案:

答案 0 :(得分:0)

将您的代码更改为:

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

每个student都有一个Profile,将其添加到表格中。

Address有多个子项,我在此代码中只添加了AddressLine1,可能需要修改它。

答案 1 :(得分:-1)

编辑原始代码 -

 <?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;   
           ?>