0
我一直在這個問題上擱置了很長時間,並希望有人可以提供幫助。這是我第一次通過Xpath方法解析SOAP。我有一個來自Bing的地理位置數據集,我試圖遍歷並寫入MYSQL表。我公司通過遍歷XML文件看起來是這樣的:無法循環使用PHP的XML Soap文件中的兒童
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<GetGeographicalLocationsResponse xmlns="http://Microsoft.BingAds.Advertiser.Campaign.MiddleTier">
<GetGeographicalLocationsResult xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.BingAds.Advertiser.CampaignManagement.MT.Messages" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Countries xmlns:b="http://schemas.datacontract.org/2004/07/Microsoft.BingAds.Advertiser.CampaignManagement.MT.Entities">
<b:Country>
<b:Latitude>39.45</b:Latitude>
<b:Licenses i:nil="true" />
<b:LocationId>190</b:LocationId>
<b:Longitude>-98.908</b:Longitude>
<b:Name>United States</b:Name>
<b:UniqueName>US</b:UniqueName>
<b:hasChildNodes>true</b:hasChildNodes>
<b:IsDistributionChannel>true</b:IsDistributionChannel>
<b:SubGeographyAreas>
<b:SubGeography>
<b:Latitude>32.7482</b:Latitude>
<b:Licenses i:nil="true" />
<b:LocationId>4080</b:LocationId>
<b:Longitude>-86.8479</b:Longitude>
<b:Name>Alabama</b:Name>
<b:UniqueName>US-AL</b:UniqueName>
<b:hasChildNodes>true</b:hasChildNodes>
<b:MetroAreas>
<b:MetroArea>
<b:Latitude>33.5231</b:Latitude>
<b:Licenses>Nielsen DMA®</b:Licenses>
<b:LocationId>71136</b:LocationId>
<b:Longitude>-86.8089</b:Longitude>
<b:Name>Birmingham, AL</b:Name>
<b:UniqueName>Birmingham, AL, AL US</b:UniqueName>
<b:hasChildNodes>true</b:hasChildNodes>
<b:Cities>
<b:City>
<b:Latitude>34.2063</b:Latitude>
<b:Licenses i:nil="true" />
<b:LocationId>42335</b:LocationId>
<b:Longitude>-87.1875</b:Longitude>
<b:Name>Addison</b:Name>
<b:UniqueName>Addison, Birmingham, AL AL US</b:UniqueName>
<b:hasChildNodes>false</b:hasChildNodes>
<b:ParentCountryLocationId>190</b:ParentCountryLocationId>
<b:ParentMetroAreaLocationId>71136</b:ParentMetroAreaLocationId>
<b:ParentSubGeographyLocationId>4080</b:ParentSubGeographyLocationId>
<b:ParentSubGeographyName>Alabama</b:ParentSubGeographyName>
</b:City>
<b:City>
<b:Latitude>32.8807</b:Latitude>
<b:Licenses i:nil="true" />
<b:LocationId>42382</b:LocationId>
<b:Longitude>-87.748</b:Longitude>
<b:Name>Akron</b:Name>
<b:UniqueName>Akron, Birmingham, AL AL US</b:UniqueName>
<b:hasChildNodes>false</b:hasChildNodes>
<b:ParentCountryLocationId>190</b:ParentCountryLocationId>
<b:ParentMetroAreaLocationId>71136</b:ParentMetroAreaLocationId>
<b:ParentSubGeographyLocationId>4080</b:ParentSubGeographyLocationId>
<b:ParentSubGeographyName>Alabama</b:ParentSubGeographyName>
</b:City>
</b:Cities>
</b:MetroArea>
</b:MetroAreas>
</b:SubGeography>
</b:SubGeographyAreas>
</b:Country>
</a:Countries>
</GetGeographicalLocationsResult>
</GetGeographicalLocationsResponse>
</s:Body>
</s:Envelope>
我的PHP代碼如下所示:
$file = 'soap-example-2.xml';
$xml = simplexml_load_file($file, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('a', 'http://schemas.datacontract.org/2004/07/Microsoft.BingAds.Advertiser.CampaignManagement.MT.Messages');
$xml->registerXPathNamespace('i', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('b', 'http://schemas.datacontract.org/2004/07/Microsoft.BingAds.Advertiser.CampaignManagement.MT.Entities');
$num_rows = count($xml->xpath('//b:Country/b:SubGeographyAreas/b:SubGeography/b:MetroAreas/b:MetroArea/b:Cities/b:City'));
echo $num_rows;
foreach($xml->xpath('//b:Country/b:SubGeographyAreas/b:SubGeography/b:MetroAreas/b:MetroArea/b:Cities/b:City') as $city)
{
for ($i=0; $i < $num_rows; $i++) {
echo $city->Name[$i];
}
}
我無法通過城市節點loopiong呼應出城的所有孩子。在我上面的php中,我只是試圖回顯每個城市名稱。如果它正常工作,將呼應了這一點:
艾迪 阿克倫
任何幫助將不勝感激!我確信這是一個簡單的事情,我忽略了。
如果這是SOAP API你用,爲什麼不使用SOAP客戶端的工作? –