php:解析xml文件多行字符串

时间:2012-06-02 20:58:27

标签: php xml

我需要为每条道路形成一个表格,每条道路都有一个shape_leng和一个坐标(多线串)列,我需要将它们保存在一行中的道路可以有任意数量的行

Xml文件具有以下格式:一些道路是多行字符串,而其他一些道路只有一行:

我试图解析它

(请注意, shape_leng 对于道路来说是单一的,但道路的坐标线可以是单个或多个。)

所以我无法按照 shape_leng 坐标等特定顺序添加它们。

1 个答案:

答案 0 :(得分:1)

如果要将所有坐标插入到单个数据库行中,我认为您必须以不同方式构造XPath和循环。循环通过道路,然后使用XPath获取属于该道路的所有坐标。例如:

// get all the roads and loop through them
$roads = $xml->xpath("//e:featureMember/b:AA_ROAD");
$i=0;

while(isset($roads[$i]))
{
    // get the coordinates for the current road
    $coordinates = $roads[i]->xpath("/b:the_geom/e:MultiLineString/e:lineStringMember/e:LineString/e:coordinates");
    $shapel = $roads[i]->xpath("/b:SHAPE_Leng");

    // add a second loop to concatinate all the $coordinates
    $j=0;
    while (isset($coordinates[$j])) {
        // TODO concatinate coordinates
    }

   // insert the row
   $b=mysql_query("INSERT IGNORE INTO `new`.`road1` (`coordstr`, `shapeleng`) values (GEOMFROMTEXT(concat('MULTILINESTRING ($a )')), '$shapel[$i]') ");


    $i++;
    echo "<br />";
    echo $i;    
}
相关问题