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

用于回显PHP的XML字符串

用于回显PHP的XML字符串,php,xml,attributes,simplexml,Php,Xml,Attributes,Simplexml,我有一个复杂的提要,希望将XML文件的值回显到PHP表中 以下是英国RailDataFeed(又名Darwin)的示例 仍然没有任何进展,在实现这一点之后,我也想将这些值添加到引导表中 如果有任何想法,请与我分享。您可以通过该方法访问属性值。另外,请注意,值在回显时转换为字符串 $str = <<<EOF <?xml version="1.0" encoding="UTF-8"?> <Pport xmlns="http://www.thalesgroup.co

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

以下是英国RailDataFeed(又名Darwin)的示例

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


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

您可以通过该方法访问属性值。另外,请注意,值在回显时转换为字符串

$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";
}
$str=位置为$loc){
回显(+$c)。“\n”;
echo(字符串)$loc->attributes()['tpl']。“\n”;
echo(字符串)$loc->arr->attributes()['at']。“\n”;
echo(字符串)$loc->dep->attributes()['et']。“\n\n”;
}
在@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";
}
?>
uR->TS->位置为$loc)
{
回显(+$c)。“\n”;
echo(字符串)$loc->attributes()['tpl']。“\n”;
echo(字符串)$loc->arr->attributes()['at']。“\n”;
echo(字符串)$loc->dep->attributes()['et']。“\n\n”;
回显(字符串)$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>

2.
8.
4.
有没有办法:

  • 忽略多个标题以避免错误
  • 将回显值发布到引导表

  • 干杯

    $xml->Pport->uR->TS->Location[0]
    关于4MB大的xml源文件,我还有一个问题。文件内部包含多个标题,是否有办法忽略标题,使其不会显示任何错误。只有在使用不同的名称空间时,普通属性才不需要
    ->attributes()
    。您可以只编写
    (字符串)$loc['tpl']
    ,而不是
    (字符串)$loc['tpl']
    。不,多个标记无效。您需要单独处理它们,并在PHP代码中组合结果。欢迎使用!如果我的答案有助于您获得XML属性,请随意投票并接受我的答案。请不要在评论或答案中发布其他问题;本网站的格式是围绕每个页面构建的,页面顶部有一个问题,下面有该问题的答案。如果尚未阅读,则可能需要通读中的和其他页面。
        $file = file_get_contents('log.xml');
    echo $file;
    
    $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";
    }
    
    <?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 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>