XML / PHP - 从多个文档节点中提取信息

时间:2015-01-01 04:33:13

标签: php xml

我想知道是否有人可以帮我解决一些代码。

我有一个KML(谷歌地球XML),我试图导入我的应用程序。

我遇到的问题是,在提取数据时,它会完美地工作直到第一个节点的东西,并且不会处理文件的其余部分。

KML文件非常庞大,所以我将在下面展示一个示例:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder>
    <name>Kalamunda</name>
    <open>1</open>
    <Document>
        <name>Export 1418786594.kml</name>
        <Placemark>
            <name>76-122 Tourist Drive 207</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.072723865509,-31.98064436184923,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>76-122 Tourist Drive 207</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0731288790703,-31.98067621355563,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>76-122 Tourist Drive 207</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0735687613487,-31.98064436184923,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>12 Hinkler Rd</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0740676522255,-31.98083319680418,0</coordinates>
            </Point>
        </Placemark>
    </Document>
    <Document>
        <name>Export 1418785221.kml</name>
        <Placemark>
            <name>LOT 435 Collins Rd</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0624188184738,-31.97887202447751,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>25 Central Rd</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0589158535004,-31.9744125903374,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>49 Canning Rd</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0574942827225,-31.97629422494352,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>13 Anne Ave</name>
            <description>Walliston WA 6076</description>
            <Point>
                <coordinates>116.0673272609711,-31.99400063250859,0</coordinates>
            </Point>
        </Placemark>
    </Document>
    <Document>
        <name>Export 1418783501.kml</name>
        <Placemark>
            <name>3 Lookout Rd</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.037417948246,-31.98043960061557,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>9 Ozone Terrace</name>
            <description>Kalamunda WA 6076</description>
            <Point>
                <coordinates>116.0382279753685,-31.98028261669366,0</coordinates>
            </Point>
        </Placemark>
    </Document>
</Folder>
</kml>

我正在使用的代码如下:

    $i = 0;
    foreach( $xml_data->Folder->Document->Placemark as $placemark ) {

        $data[$i][] = $placemark->name;
        $data[$i][] = $placemark->description;

        list($x, $y) = explode(',', $placemark->Point->coordinates);
        $data[$i][] = array($x, $y);

        $i++;

    }

使用此代码,它将提取数据直到12 Hinkler Rd地标,我认为因为有一个结束标记,它不会再进一步​​了。

如何以这种格式提取XML格式的所有数据?

非常感谢任何帮助。

干杯,

1 个答案:

答案 0 :(得分:2)

使用XPath查询最容易获得所有<Placemark>个节点。由于它们位于kml命名空间下,因此您可以查询//kml:Placemark//将在XML中的任何级别搜索表达式Placemark

我们可以通过巧妙地将array_slice()与您已有的explode()结合使用,大大简化您原来循环的内部工作原理。将explode()嵌套在array_slice()内,检索前两个坐标组件并丢弃第三个坐标组件(,0)可以在一个操作中发生,并立即将它们分配给数组键['coordinates']

此外,您可以use the [] array append syntax直接将新的子数组直接粘贴到$i集合数组上,而不是保持递增$data

// Query all <Placemark> elements at once
$placemarks = $xml_data->xpath('//kml:Placemark');
// Loop over them and construct an array of child properties

$data = array();
foreach ($placemarks as $mark) {
  // Collect the properties into a new array and append
  // that to $data with []
  $data[] = array(
    // Cast SimpleXML elements to strings with (string)
    'name' => (string)$mark->name,
    'description' => (string)$mark->description,
    // Get the coords in one action - explode on the ','
    // but return only the first two elements via array_slice()
    'coordinates' => array_slice(explode(',', $mark->Point->coordinates), 0, 2)
  );
}
print_r($data);
// Displays:
Array
(
    [0] => Array
        (
            [name] => 76-122 Tourist Drive 207
            [description] => Kalamunda WA 6076
            [coordinates] => Array
                (
                    [0] => 116.072723865509
                    [1] => -31.98064436184923
                )

        )

    [1] => Array
        (
            [name] => 76-122 Tourist Drive 207
            [description] => Kalamunda WA 6076
            [coordinates] => Array
                (
                    [0] => 116.0731288790703
                    [1] => -31.98067621355563
                )

        )

    [2] => Array
        (
            [name] => 76-122 Tourist Drive 207
            [description] => Kalamunda WA 6076
            [coordinates] => Array
                (
                    [0] => 116.0735687613487
                    [1] => -31.98064436184923
                )

        )
    ... SNIP.... 
    ... SNIP.... 
    ... SNIP....    
    [8] => Array
        (
            [name] => 3 Lookout Rd
            [description] => Kalamunda WA 6076
            [coordinates] => Array
                (
                    [0] => 116.037417948246
                    [1] => -31.98043960061557
                )

        )

    [9] => Array
        (
            [name] => 9 Ozone Terrace
            [description] => Kalamunda WA 6076
            [coordinates] => Array
                (
                    [0] => 116.0382279753685
                    [1] => -31.98028261669366
                )

        )

)