[英]Reading Data from XML to HTML Table
嗨,我正在嘗試將記錄從XML文件讀取到HTML表中。 我正在嘗試實現的代碼無法正常工作,僅顯示空白頁。 你能幫我哪里錯了嗎?
這是我的XML文件數據:
<staff><info><staffid>test</staffid><email>testtest@gtes.com</email><surname>test</surname><givename>test</givename><address>test</address></info></staff>
這是我要顯示html記錄的PHP / HTML代碼。
<?php
$file="staff.xml";
$xml = simplexml_load_file($file) or die ("Error Loading Data");
foreach($xml->staff as $staff){
?>
<tbody><tr>
<td><?php echo $staff->staffid; ?></td>
<td><?php echo $staff->email; ?></td>
<td><?php echo $staff->surname; ?></td>
<td><?php echo $staff->givename; ?></td>
<td><?php echo $staff->address; ?></td>
</tr>
</tbody>
<?php
}
?>
您在解析該xml時具有incoreact路徑,更改您的foreach循環
foreach($xml->staff as $staff)
同
foreach ($xml->info as $stafs)
我工作的測試代碼
$url = 'test.xml';
$data = file_get_contents($url);
$xml = simplexml_load_string($data);
$enc = json_encode($xml);
foreach ($xml->info as $staff) {
echo $staff->staffid;
}
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.