用于回显PHP的XML字符串

时间:2018-01-04 03:37:29

标签: php xml attributes simplexml

我有一个复杂的feed,我希望将XML文件的值回显到PHP表。

以下是英国RailDataFeed(Aka Darwin)的样本。

<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-01T21:58:48.2213864Z" version="12.0">
    <uR updateOrigin="Trust">
        <TS>
            <Location pta="21:59" ptd="21:59" tpl="ROBY" wta="21:59" wtd="21:59:30">
                <arr at="21:59" src="TRUST" srcInst="Auto" />
                <dep et="21:59" src="Darwin" />
                <plat conf="true" platsrc="A">4</plat>
            </Location>
            <Location pta="22:06" ptd="22:06" tpl="PRESCOT" wta="22:05:30" wtd="22:06">
                <arr et="22:06" src="Darwin" wet="22:05" />
                <dep et="22:06" src="Darwin" />
                <plat>1</plat>
            </Location>
        </TS>
    </uR>
</Pport>

我一直在努力和研究整天试图让回声发挥作用,但没有运气,我尝试了以下

测试(1)

$xml=simplexml_load_file("log.xml") or die("Error: Cannot create object");
echo $xml->Location[0]['tpl'] . "<br>";
echo $xml->Location[1]->arr['at']; 
echo $xml->Location[2]->dep['et']; 
echo $xml->Location[3]->plat;

测试(2)

$xml=simplexml_load_file("log.xml") or die("Error: Cannot create object");
foreach($xml->children() as $loc) { 
    echo $loc->Location['tpl'];
    echo $loc->arr['at'];
    echo $loc->plat;
    echo "<br>"; 
} 

测试(3)

    $file = file_get_contents('log.xml');
echo $file;

仍然没有任何进展,在实现这一点后,我想将值添加到引导表中。

如果有任何想法请与我分享。

2 个答案:

答案 0 :(得分:0)

您可以通过attributes()方法访问属性值。另请注意,回显时会将值强制转换为字符串。

$str = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-01T21:58:48.2213864Z" version="12.0">
    <uR updateOrigin="Trust">
        <TS>
            <Location pta="21:59" ptd="21:59" tpl="ROBY" wta="21:59" wtd="21:59:30">
                <arr at="21:59" src="TRUST" srcInst="Auto" />
                <dep et="21:59" src="Darwin" />
                <plat conf="true" platsrc="A">4</plat>
            </Location>
            <Location pta="22:06" ptd="22:06" tpl="PRESCOT" wta="22:05:30" wtd="22:06">
                <arr at="22:06" src="Darwin" wet="22:05" />
                <dep et="22:06" src="Darwin" />
                <plat>1</plat>
            </Location>
        </TS>
    </uR>
</Pport>
EOF;

$xml = simplexml_load_string($str);

$c = 0;
foreach($xml->uR->TS->Location as $loc) {
    echo (++$c) . "\n";
    echo (string)$loc->attributes()['tpl'] . "\n";
    echo (string)$loc->arr->attributes()['at'] . "\n";
    echo (string)$loc->dep->attributes()['et'] . "\n\n";
}

答案 1 :(得分:0)

@marekful回复后。

代码修改为

<?php

$str = file_get_contents('log.xml');
$xml = simplexml_load_string($str);
$c = 0;
foreach($xml->uR->TS->Location as $loc) 
{
    echo (++$c) . "\n";
    echo (string)$loc->attributes()['tpl'] . "\n";
    echo (string)$loc->arr->attributes()['at'] . "\n";
    echo (string)$loc->dep->attributes()['et'] . "\n\n";
    echo (string)$loc->plat . "\n\n\n";
}
?>

它与示例xml文件完美配合,但在添加原始XML文件后,它在文件本身中有多个标题,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:28.3036616Z" version="12.0">
    <uR requestID="AM02050384" requestSource="AM02" updateOrigin="CIS">
        <TS rid="201801037171519" ssd="2018-01-03" uid="G71519">
            <ns3:Location tpl="GLYNDE" wtp="01:25:08">
                <ns3:pass at="01:31" src="TD" />
                <ns3:plat conf="true" platsrc="A" platsup="true">2</ns3:plat>
                <ns3:length>8</ns3:length>
            </ns3:Location>
        </TS>
    </uR>
</Pport>
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:29.1772672Z" version="12.0">
    <uR requestID="0000000000046386" requestSource="at21" updateOrigin="CIS">
        <TS rid="201801038706030" ssd="2018-01-03" uid="W06030">
            <ns3:Location pta="01:25" ptd="01:26" tpl="DARTFD" wta="01:25" wtd="01:26">
                <ns3:arr at="01:31" src="TD" />
                <ns3:dep et="01:32" etmin="01:27" src="Darwin" />
                <ns3:plat conf="true" platsrc="A">4</ns3:plat>
            </ns3:Location>
        </TS>
    </uR>
</Pport>
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:30.1912737Z" version="12.0">
    <uR updateOrigin="TD">
        <TS rid="201801027160109" ssd="2018-01-02" uid="G60109">
            <ns3:Location tpl="BRINKLW" wtp="01:34:30">
                <ns3:pass at="01:31" src="TD" /></ns3:Location>
        </TS>
    </uR>
</Pport>
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:31.2052802Z" version="12.0">
    <uR updateOrigin="TD">
        <TS rid="201801036763188" ssd="2018-01-03" uid="C63188">
            <ns3:Location tpl="AMBERGJ" wtp="02:04:30">
                <ns3:pass et="01:38" src="TD" /></ns3:Location>
        </TS>
    </uR>
</Pport>

有办法:

  1. 忽略多个标头以避免错误。
  2. 将回显值发布到Bootstrap表。
  3. 干杯!