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

XML / PHP - Extracting information from multiple document nodes

我想知道是否有人可以帮助我编写一些代码。

我有一个 KML ( Google Earth 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 这种格式,我将如何提取所有数据?

如有任何帮助,我们将不胜感激。

干杯,

所有 <Placemark> 节点最容易通过 XPath 查询获得。由于它们位于 kml 命名空间下,您可以查询 //kml:Placemark// 将在 XML.

的任何级别搜索表达式 Placemark

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

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

// 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
                )

        )

)