开发者

Issue with parsing some elements out of a KML file

I have an issue with trying to parse out bits of data using PHP from a KML file that I have generated and then sent to my webserver. I want to get the elements out, and store them to a database. Storing them isn't an issue, its just getting some elements out.

Here is en example of the KML file im trying to parse:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http开发者_如何学Go://www.opengis.net/kml/2.2">
    <Document>
        <name>RandomName</name>
        <description>-</description>
        <Style id="line">
            <LineStyle>
            <color>CD0000FF</color>
            <width>4</width>
            </LineStyle>
        </Style>
        <ExtendedData>
            <Data name="StartTime">
                <value>1302653118875</value>
            </Data>
            <Data name="EndTime">
                <value>1302653159274</value>
            </Data>
            <Data name="Type">
                <value>cycle</value>
            </Data>
        </ExtendedData>

    <Placemark>
        <name>PlaceName2</name>
        <description><p>Current Altitude: <b>0.0 ft</b><br/>Current Speed: <b>0.0 m/s</b><br/>Total Distance: <b>0.608 km</b></p></description>
        <styleUrl>#line</styleUrl>
        <LineString>
            <extrude>1</extrude>
            <tessellate>1</tessellate>
            <coordinates>
            -122.083393,37.427768,0.0
            -122.084395,37.427768,0.0
            </coordinates>
        </LineString>
    </Placemark>
</Document>
</kml>

I am using the following code, and I am able to get out the very first name and description.

function getTrackDescription($file){
    $xml = simplexml_load_file($file);
    $namespace = $xml->getDocNamespaces();

    if(isset($namespace[""])){
        $xml->registerXPathNamespace("default",$namespace[""]);
    }

    $element= $xml->xpath('//default:description');
    return $element[0];
}

I am able to get these elements but when I attempt to get anything else, I just get nothing. I need to be able to access the values within ExtendedData - StartTime, EndTime and Type. I also need to get the description from Placemark.

I changed 1 line from the above code, in attempting to get the StartTime, but it doesn't work.

$element= $xml->xpath('//default:ExtendedData/Data[name="StartTime"]/value');

If anyone could help me out, or point me in the right direction I would be really greatful as I haven't really used XML to much.

Thank you.


Since you've defined a prefix for the namespace "http://www.opengis.net/kml/2.2" you have to make sure that you use it for all elements falling into that namespace. In the code above, you're essentially looking for ExtendedData with the prefix default, but then looking for Data elsewhere in a namespace with no prefix.

Here are your options:

$xml = simplexml_load_file($file);
var_dump($xml->xpath("//ExtendedData/Data[@name='StartTime']/value"));

or

$xml = simplexml_load_file($file);
$xml->registerXPathNamespace("default","http://www.opengis.net/kml/2.2");
var_dump($xml->xpath("//default:ExtendedData/default:Data[@name='StartTime']/default:value"));

Both should return the items that you're hoping for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜