2014-01-13 1 views
0

나는이 문제에 오랫동안 곤두박질 당했고 누군가가 도울 수 있기를 바랬습니다. 이것은 Xpath 메서드를 통해 SOAP을 처음으로 파싱하는 것입니다. Bing의 Geo Locations 데이터 세트를 가지고 있으며이를 반복하고 MYSQL 테이블에 쓰려고합니다.PHP를 사용하여 XML 비누 파일을 반복 할 수 없습니다.

<?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 문제가 발생하고 난을 반복하고 XML 파일은 다음과 같이 보입니다 . 위의 PHP에서는, 나는 단순히 각 도시 이름을 반향하려고합니다. 제대로 작동 된 경우이 밖으로 에코 것 :

애디슨 이 애 크론

어떤 도움을 크게 감상 할 수있다! 나는 이것이 내가 간과하고있는 간단한 것임을 확신한다.

+1

SOAP API를 사용하고 있다면 왜 SOAP 클라이언트를 사용해야합니까? –

답변

1

내부의 용도가 보이지 않아 루프 for가 표시됩니다. 각 도시에는 Name 중 하나만 있습니다. 맞습니까? 이것을 시도한 적이 있습니까?

$bNs = 'http://schemas.datacontract.org/2004/07/Microsoft.BingAds.Advertiser.CampaignManagement.MT.Entities'; 
$cityPath = '//b:Country/b:SubGeographyAreas/b:SubGeography/b:MetroAreas/b:MetroArea/b:Cities/b:City'; 
foreach($xml->xpath($cityPath) as $city) 
{ 
    echo $city->children($bNs)->Name; 
} 
+0

는 매력처럼 작동했습니다 - 감사합니다! –

관련 문제