使用PHP回显XML文件内容

时间:2012-08-23 00:53:29

标签: php xml xmltable

我想使用file_get_contents($xml) ...

以简单的表格格式回显以下XML文件
<CallOverview>
<Calls Count="3">
<Call StartTime="10:26:25  (UTC)" Destination="+12345" Duration="00:02:25" Charge="0.039"/>
<Call StartTime="10:22:04  (UTC)" Destination="+12345" Duration="00:01:20" Charge="0.026"/>
<Call StartTime="10:08:28  (UTC)" Destination="+12345" Duration="00:02:24" Charge="0.039"/>
</Calls>
<MoreData>True</MoreData>
</CallOverview>

要转换为...的简单表格式

Calls Count: 3

Start Time    Destination    Duration    Charge
10:26:25      +12345         00:02:25    0.039
10:22:04      +12345         00:01:20    0.026
10:08:28      +12345         00:02:24    0.039

1 个答案:

答案 0 :(得分:0)

检查一下。

$xmltext = '<CallOverview>
                <Calls Count="3">
                <Call StartTime="10:26:25  (UTC)" Destination="+12345" Duration="00:02:25" Charge="0.039"/>
                <Call StartTime="10:22:04  (UTC)" Destination="+12345" Duration="00:01:20" Charge="0.026"/>
                <Call StartTime="10:08:28  (UTC)" Destination="+12345" Duration="00:02:24" Charge="0.039"/>
                </Calls>
                <MoreData>True</MoreData>
                </CallOverview>';
$xml = simplexml_load_string($xmltext);
foreach($xml->Calls->Call as $call)
{
    $attributes = $call->attributes();
    echo $attributes['StartTime'];
}

我认为您可以改进此代码示例并完成您的工作。