无法使用PHP在XML Soap文件中循环遍历子项

时间:2014-01-13 17:02:04

标签: php xml soap

我长期以来一直对这个问题感到难过,并希望有人可以提供帮助。这是我第一次通过Xpath方法解析SOAP。我有Bing的Geo Locations数据集,我试图迭代并写入MYSQL表。我正在迭代的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];
    }
}

我无法通过城市节点回响所有城市的孩子。在我上面的php中,我只想回应每个城市名称。如果它正常工作,它会回应这个:

艾迪生 阿克伦

任何帮助将不胜感激!我确信这是一个我忽略的简单事情。

1 个答案:

答案 0 :(得分: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;
}