php - 如何从soap xml响应中获取城市
问题描述
嗨,我正在使用 curl 来获得肥皂响应。我试图从下面的soap xml响应中获取城市是我得到的xml响应。
`<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getStationsResponse xmlns="http://www.jimpisoft.pt/Rentway_Reservations_WS/getStations">
<getStationsResult>
<stations>
<xs:schema id="RentalStations" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="RentalStations" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table">
<xs:complexType><xs:sequence><xs:element name="StationID" type="xs:string" minOccurs="0" /><xs:element name="Station" type="xs:string" minOccurs="0" />
<xs:element name="Zone" type="xs:int" minOccurs="0" />
<xs:element name="StationType" type="xs:int" minOccurs="0" />
<xs:element name="CountryID" type="xs:int" minOccurs="0" />
<xs:element name="City" type="xs:string" minOccurs="0" />
<xs:element name="SupplierID" type="xs:int" minOccurs="0" />
<xs:element name="Latitude" type="xs:decimal" minOccurs="0" />
<xs:element name="Longitude" type="xs:decimal" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<RentalStations xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<StationID>MBAC01</StationID>
<Station>Mombasa Trade Center </Station>
<Zone>1</Zone>
<StationType>1</StationType>
<CountryID>1</CountryID>
<City>MOMBASA</City>
<Latitude>0.00000</Latitude>
<Longitude>0.00000</Longitude>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<StationID>NBOT01</StationID>
<Station>Nairobi Airport JKIA </Station>
<Zone>1</Zone>
<StationType>2</StationType>
<CountryID>1</CountryID>
<City>NAIROBI</City>
<Latitude>0.00000</Latitude>
<Longitude>0.00000</Longitude>
</Table>
</RentalStations>
</diffgr:diffgram>
</stations>
</getStationsResult>
</getStationsResponse>
</soap:Body>
</soap:Envelope>`
我正在使用下面的代码来获取城市
$parser = simplexml_load_string($response4);
foreach($parser->getStationsResponse->getStationsResult->stations->diffgr->RentalStations->Table as $od){
$abf = $od->city;
echo "city name is".$abf;
}
有人可以告诉我,我在哪里做错了。我尝试了很多方法来获得结果。
还有没有其他方法可以得到结果。
解决方案
您可以使用 xpath 从 XML 中获取城市。
$parser = simplexml_load_string($response4);
$rentalStations = $parser->xpath('//RentalStations/Table');
$cities = [];
foreach ($rentalStations as $rentalStation) {
$cities[] = (string)$rentalStation->City;
}
var_dump($cities);
推荐阅读
- android - 我们可以从 Android Surface 获取文件描述符吗?
- push-notification - 在 swift 应用程序中点击推送消息时打开视图
- go - .Net 的 Marshal.Copy 方法的 Golang 等价物是什么?
- .net - 作为后端的 .NET Core 应用程序与部署在 Docker 上的 Nginx 服务器上的应用程序之间没有通信
- powershell - 文字路径不起作用。我究竟做错了什么?
- python - 找不到文件的问题
- firefox - Firefox 架构中的“webRequest”拦截如何工作?
- html - 'int' 对象不可迭代。如何解决这个问题呢?
- wordpress - 在 Wordpress 中上传特定于用户的 PDF 文件
- excel - Excel VBA,创建PPT演示文稿。每次执行代码都会出现不同的错误