-2

来源:

使用 php simplexml 显示来自 XML 的数据

使用 SimpleXML 获取属性和值

PHP SimpleXML + 获取属性

我正在尝试使用 simpleXML for php 但无法使其正常工作

我的 xml 示例

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <gpx creator="Ski Tracks 1.3.3" version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
    <metadata>
    <link href="http://www.corecoders.com">
    <text>Core Coders Ltd</text>
    </link>
    <time>2012-11-15T13:52:46.005+01:00</time>
    <bounds maxlat="52.0502500" maxlon="5.6405950" minlat="52.0498680" minlon="5.6387020"/>
    </metadata>
    <trk>
    <name>New Track Doe Maar</name>
    <desc>Yeah</desc>
    <type>snowboarding</type>
    <trkseg>
    <trkpt lat="52.0499970" lon="5.6387520">
    <ele>3.00</ele>
    <time>2012-11-15T13:42:17.380+01:00</time>
    </trkpt>
    <trkpt lat="52.0499510" lon="5.6388820">
    <ele>3.00</ele>
    <time>2012-11-15T13:43:32.433+01:00</time>
    </trkpt
</trkpt>
</trkseg>
</trk>
 </gpx>

我想检索 'trkpt' 属性及其子元素,例如 'time' 和 'ele',并且我想将 trkpt 'lat'/'lon'、'time' 和 'ele' 元素放在一个数组中。我只是不让它工作这件作品现在工作正常并获取元数据信息

    $xml = simplexml_load_file("./data/ski1/skitrack.xml");


    //$url = './data/ski1/skitrack.xml';
    //$xml = new SimpleXmlElement($url, null, true);

    $time = $xml->metadata[0]->time;
    $name = $xml->trk->name;
    $desc = $xml->trk->desc;
    $type = $xml->trk->type;
    foreach ($xml->metadata[0]->bounds as $bounds) {
        $maxlat = $bounds->attributes()->maxlat;
        $maxlon = $bounds->attributes()->maxlon;
        $minlat = $bounds->attributes()->minlat;
        $minlon = $bounds->attributes()->minlon; 

    }

但是当我为 trkpt 尝试它时,它给了我一个错误,即没有数组:为 foreach 提供的参数无效

    print_r($xml);


    foreach ($xml->trkseg->trkpt as $trkpt) {
        $lon = $trkpt->attributes()->lon;
    }

我实际上正在尝试将代码从 javascript 转换为 php,它在 javascr 中工作得很好

        var alat = new Array();
        var alon = new Array();
        var ele = new Array();
        var year = new Array();
        var month = new Array();
        var day = new Array();
        var hours = new Array();
        var minutes = new Array();
        var seconds = new Array();
        var milisec = new Array();

        $(xml).find('trkseg').each(function(){
            $(this).find('trkpt').each(function(i){
                alat[i] = $(this).attr('lat');
                alon[i] = $(this).attr('lon');
                ele[i] = $(this).find('ele').text();
                year[i] = $(this).find('time').text().substr(0,4);
                month[i] = $(this).find('time').text().substr(5,2);
                day[i] = $(this).find('time').text().substr(8,2);
                hours[i] = $(this).find('time').text().substr(11,2);
                minutes[i] = $(this).find('time').text().substr(14,2);
                seconds[i] = $(this).find('time').text().substr(17,2);
                milisec[i] = $(this).find('time').text().substr(20,3);

                if(i > 0) {
                    var laterdate = new Date(year[i],month[i],day[i],hours[i],minutes[i],seconds[i],milisec[i]);
                    var earlierdate = new Date(year[i-1],month[i-1],day[i-1],hours[i-1],minutes[i-1],seconds[i-1],milisec[i-1]);
                    locDistance(alat[i-1], alon[i-1], alat[i], alon[i]);
                    timeDifference(laterdate,earlierdate);
                }

                $('.content').append(i);
                $('.content').append(
                '<br />lat ' + alat[i] +
                '<br />' + 'lon ' + alon[i] +
                '<br / >' + 'ele ' + ele[i] +
                '<br / >' + 'time ' + year[i] + (month[i]-1) + day[i] + hours[i] + minutes[i] + seconds[i] + milisec[i] +
                '<br / >' + 'dist ' + d + '<br / >'
                );

                i = i + 1;

            });
        });

先感谢您

4

1 回答 1

1

虽然您发布的 xml 没有验证(它缺少标签),但我假设它应该是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx creator="Ski Tracks 1.3.3" version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
   <metadata>
      <link href="http://www.corecoders.com">
         <text>Core Coders Ltd</text>
      </link>
      <time>2012-11-15T13:52:46.005+01:00</time>
      <bounds maxlat="52.0502500" maxlon="5.6405950" minlat="52.0498680" minlon="5.6387020"/>
   </metadata>
   <trk>
      <name>New Track Doe Maar</name>
      <desc>Yeah</desc>
      <type>snowboarding</type>
      <trkseg>
         <trkpt lat="52.0499970" lon="5.6387520">
            <ele>3.00</ele>
            <time>2012-11-15T13:42:17.380+01:00</time>
         </trkpt>
         <trkpt lat="52.0499510" lon="5.6388820">
            <ele>3.00</ele>
            <time>2012-11-15T13:43:32.433+01:00</time>
         </trkpt>
      </trkseg>
   </trk>
</gpx>

如果是这种情况,那么您的代码foreach ($xml->trkseg->trkpt无法找到,trkseg因为它嵌套在trk元素中。相反,您可以尝试遍历每个trk元素,然后在每个trk元素中访问这些trkseg元素。

foreach($xml->trk as $trk)
{
    //do something with each trk

    foreach ($trk->trkseg->trkpt as $trkpt) {

         //do something with each trkseg

    }
} 
于 2012-11-18T00:28:16.673 回答