PHP:用GML标签解析XML文件



我需要帮助!我试图用php解析一个带有GML标签的XML文件。我使用simplexml_load_file函数来解析我的文件,它工作得很好,除了gml: tags。

文件如下:

<par>
.....
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>
{{ DATAS I WOULD LIKE TO PARSE}}
</gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
......
</par>

下面是我使用的代码:

<?php
if (file_exists('doc.xml')) {
$xml = simplexml_load_file('doc.xml');
} 
...    
foreach ($xml->...->par->{'gml:Polygon'}->{'gml:outerBoundaryIs'}->{'gml:LinearRing'}->{'gml:coordinates'} as $coords) {
echo $coords;
echo '<br>';   
}

运行代码,我得到:

警告:试图读取属性"gml:LinearRing"在wwwxmlphptestindex.php第44行

警告:试图读取属性"gml:coordinates"在wwwxmlphptestindex.php第44行

警告:foreach()参数必须是array类型|object, null在第44行wwwxmlphptestindex.php中给出

你能帮帮我吗?谢谢:)

在php文档中找到。与xml命名空间相关:https://www.php.net/manual/fr/simplexmlelement.registerxpathnamespace.php

感谢