使用SimpleXMLparser PHP分析具有名称空间的xml [英] Parse xml with namespaces with SimpleXMLparser php

查看:64
本文介绍了使用SimpleXMLparser PHP分析具有名称空间的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析这样的XML:

I am trying to parse a XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<gml:FeatureCollection 
    xmlns:ogc="http://www.opengis.net/ogc" 
    xmlns:gml="http://www.opengis.net/gml"
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:wfs="http://www.opengis.net/wfs"
    xmlns:p="http://example.org">
    <gml:featureMember>
        <p:Point>
            <gml:pointProperty>
                <gml:Point srsName="epsg:4258">
                    <gml:pos>-3.84307585 43.46031547</gml:pos>
                </gml:Point>
                <gml:Point srsName="epsg:4258">
                    <gml:pos>-3.84299411 43.46018513</gml:pos>
                </gml:Point>
                <gml:Point srsName="epsg:4258">
                    <gml:pos>-3.84299935 43.45998723</gml:pos>
                </gml:Point>
                <!-- 
                    ... many more <gml:Point> nodes ...
                --> 
                <gml:Point srsName="epsg:4258">
                    <gml:pos>-3.84309913 43.46054546</gml:pos>
                </gml:Point>
                <gml:Point srsName="epsg:4258">
                    <gml:pos>-3.84307585 43.46031547</gml:pos>
                </gml:Point>
            </gml:pointProperty>
        </p:Point>
    </gml:featureMember>
</gml:FeatureCollection>

我想将每个gml:pos行保存到数据库中,但目前我很高兴在webpace(echo ...)中打印它们

I want to get each of gml:pos rows to save to a DB but for the moment I am happy printing them in webpace (echo...)

$output = simplexml_load_string($output);
$xml = $output->getNamespaces(true); 
//print_r( $xml);
$xml_document = $output->children($xml["p"]);
foreach($xml_document->Point->children($xml["gml"]);
    echo $xml_point->Point[0];
echo $xml->FeatureCollection; 
}

$ output 中,我有完整的xml,在gml:point

In $output I have the complete xml, tons of coordinates in gml:point

但是我试图使用名称空间来说明问题,但是我必须做错什么,因为我只能打印数组字(即使使用print_r ...)也不能打印任何东西

But I am trying to get to the points using namespaces but I have to be doing something wrong because I can't print anything but Array word (even by using print_r...)

推荐答案

您不应从文档中读取名称空间.命名空间是一个唯一的字符串,用于定义标签所属的XML语义. XML是一个很好的例子,因为它在两个不同的命名空间中具有Point元素.

You should not read the namespaces from the document. The namespace is a unique string defining the XML semantic the tag is part of. Your XML is a good example for that, because it has Point elements in two different namespaces.

p:Point是{ http://example.org }:点 gml:Point是{ http://www.opengis.net/gml }:点

p:Point is {http://example.org}:Point gml:Point is {http://www.opengis.net/gml}:Point

名称空间前缀(例如pgml)是使文档更小,更易读的别名.它们仅对元素及其子元素有效.可以在任何时候重新定义它们.更重要的是,它们仅对文档有效.

The namespace prefixes like p and gml are aliases to make a document smaller and more readable. They are only valid for the element and its children. They can be redefined at any point. More important they are only valid for the document.

因此,要读取XML,您需要为命名空间定义自己的前缀,并将其与Xpath一起使用,或者使用DOM方法(如getAttributeNS())的命名空间感知型.从长远来看,Xpath是更优雅的解决方案.您可以使用文档中的前缀,也可以使用其他前缀.

So to read XML you define own prefixes for the namespaces and use them with Xpath or you use the namespace aware variants of the DOM methods like getAttributeNS(). Xpath is by a long way the more elegant solution. You can use the prefixes from the document or different ones.

$element = simplexml_load_string($content);
$element->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
$element->registerXPathNamespace('p', 'http://example.org');

$result = [];
$positions = $element->xpath('//p:Point[1]//gml:pos');
foreach ($positions as $pos) {
  $result[] = (string)$pos;
}

var_dump($result);

输出: https://eval.in/159739

array(5) {
  [0]=>
  string(23) "-3.84307585 43.46031547"
  [1]=>
  string(23) "-3.84299411 43.46018513"
  [2]=>
  string(23) "-3.84299935 43.45998723"
  [3]=>
  string(23) "-3.84309913 43.46054546"
  [4]=>
  string(23) "-3.84307585 43.46031547"
}

这篇关于使用SimpleXMLparser PHP分析具有名称空间的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆