如何在PHP中解析XML并提取posList值到数组?
XML转PHP数组:提取posList值
我对XML并不熟悉,一直尝试将XML文件中的数据解析到PHP数组中,但进展不大。目前已实现提取predefinedLocationReference的id和名称,但缺少提取posList值并添加到数组的方法。
XML内容
<ns2:messageContainer xmlns="datex2.eu/schema/3/common" xmlns:ns2="datex2.eu/schema/3/messageContainer" xmlns:ns3="datex2.eu/schema/3/exchangeInformation" xmlns:ns4="datex2.eu/schema/3/informationManagement" xmlns:ns5="datex2.eu/schema/3/dataDictionaryExtension" xmlns:ns6="datex2.eu/schema/3/cctvExtension" xmlns:ns7="datex2.eu/schema/3/locationReferencing" xmlns:ns8="datex2.eu/schema/3/alertCLocationCodeTableExtension" xmlns:ns9="datex2.eu/schema/3/extension" xmlns:ns10="datex2.eu/schema/3/roadTrafficData" xmlns:ns11="datex2.eu/schema/3/vms" xmlns:ns12="datex2.eu/schema/3/situation" modelBaseVersion="3"> <ns2:payload xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns7:PredefinedLocationsPublication" lang="no" modelBaseVersion="3"> <publicationTime>2023-01-25T13:56:15.615+01:00</publicationTime> <publicationCreator> <country>no</country> <nationalIdentifier>NPRA</nationalIdentifier> </publicationCreator> <ns7:headerInformation> <confidentiality>noRestriction</confidentiality> <informationStatus>real</informationStatus> </ns7:headerInformation> <ns7:predefinedLocationReference xsi:type="ns7:PredefinedLocation" id="100356" version="1"> <ns7:predefinedLocationName> <values> <value lang="no">Eikås - Åsanevegen</value> </values> </ns7:predefinedLocationName> <ns7:location xsi:type="ns7:LinearLocation"> <ns7:gmlLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#32633"> <ns7:posList>-27946 6743813</ns7:posList> </ns7:gmlLineString> </ns7:location> </ns7:predefinedLocationReference> <ns7:predefinedLocationReference xsi:type="ns7:PredefinedLocation" id="100361" version="1"> <ns7:predefinedLocationName> <values> <value lang="no">Ammerud - Bjerke</value> </values> </ns7:predefinedLocationName> <ns7:location xsi:type="ns7:LinearLocation"> <ns7:gmlLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#32633"> <ns7:posList>269553 6653843</ns7:posList> </ns7:gmlLineString> </ns7:location> </ns7:predefinedLocationReference> </ns2:payload> <ns2:exchangeInformation modelBaseVersion="3"> <ns3:exchangeContext> <ns3:codedExchangeProtocol>snapshotPull</ns3:codedExchangeProtocol> <ns3:exchangeSpecificationVersion>3</ns3:exchangeSpecificationVersion> <ns3:supplierOrCisRequester> <ns3:internationalIdentifier> <country>no</country> <nationalIdentifier>NPRA</nationalIdentifier> </ns3:internationalIdentifier> </ns3:supplierOrCisRequester> </ns3:exchangeContext> <ns3:dynamicInformation> <ns3:exchangeStatus>undefined</ns3:exchangeStatus> <ns3:messageGenerationTimestamp>2023-01-25T13:56:15.615+01:00</ns3:messageGenerationTimestamp> </ns3:dynamicInformation> </ns2:exchangeInformation> </ns2:messageContainer>
现有PHP代码
$xml = simplexml_load_string($response->raw_body, "SimpleXMLElement", LIBXML_NOCDATA, 'ns2', true); $xml->registerXPathNamespace('ns7','http://datex2.eu/schema/3/locationReferencing'); $count = 0; foreach($xml->xpath('//ns7:predefinedLocationReference') as $event) { $return[$count]['id'] = intval($event->attributes()->id); $predefinedLocationName = $event->xpath('ns7:predefinedLocationName'); foreach ($predefinedLocationName[0]->values as $locVal) { $return[$count]['name'] = strval($locVal->value); } $count++; }
当前输出结果
[ { "id": 100356, "name": "Eikås - Åsanevegen" }, { "id": 100361, "name": "Ammerud - Bjerke" } ]
解决方案
在现有循环中添加提取posList的逻辑,利用已注册的命名空间通过XPath定位节点,提取文本值后可直接存入数组,或拆分坐标为子数组:
$xml = simplexml_load_string($response->raw_body, "SimpleXMLElement", LIBXML_NOCDATA, 'ns2', true); $xml->registerXPathNamespace('ns7','http://datex2.eu/schema/3/locationReferencing'); $return = []; foreach($xml->xpath('//ns7:predefinedLocationReference') as $event) { $item = []; // 提取ID $item['id'] = intval($event->attributes()->id); // 提取名称 $predefinedLocationName = $event->xpath('ns7:predefinedLocationName'); if (!empty($predefinedLocationName)) { $item['name'] = strval($predefinedLocationName[0]->values->value); } // 提取posList值 $posListNodes = $event->xpath('ns7:location/ns7:gmlLineString/ns7:posList'); if (!empty($posListNodes)) { $posValue = trim(strval($posListNodes[0])); // 可选1:直接存储字符串 // $item['posList'] = $posValue; // 可选2:拆分为坐标数组 $item['posList'] = explode(' ', $posValue); } $return[] = $item; }
修改后输出结果
[ { "id": 100356, "name": "Eikås - Åsanevegen", "posList": ["-27946", "6743813"] }, { "id": 100361, "name": "Ammerud - Bjerke", "posList": ["269553", "6653843"] } ]
内容的提问来源于stack exchange,提问作者JIMJA
相关产品推荐
相关产品推荐

