必应简单 XML 对象条目遍历如何



>我从BING(注意:复合结果)获得以下XML字符串作为响应,我尝试并尝试扫描条目(内联>feed->条目)到for循环,但失败了。 我用来扫描结果的代码如下

$xml = new SimpleXMLElement($rs);
$i=0;       
if ( $xml->entry->link ) {
    $feeds = $xml->entry->link->children('m', TRUE)->inline->entry;
    foreach ( $feeds as $results) {
        $i++;
        echo $data=(string)$results->content;
        $result = $data->children('m', TRUE)->properties->children('d', TRUE);
        echo "ss".$clickurl = $result->Url;
        $url = urldecode($clickurl);
        $search[$i]['url'] = str_replace("&", "&", $url);// for the vali
        $search[$i]['abstract'] = (string)$result->Description;
        $search[$i]['title']    = (string)$result->Title;
        $search[$i]['rank'] = $i;   
    } //foreach 
}
return $search;

*你能告诉我我在这里错过了什么吗? 我不知道如何使用simpleXML对象访问<m:inline> <feed>数据集,微软支持团队的回答是PHP不是他们的语言,所以他们无法帮助我,要求我使用论坛/stackoverflow。*

   <feed xmlns:base="https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Composite" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">keyword</title>
  <subtitle type="text">Bing Search API</subtitle>
  <id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Composite?Query='keyword'</id>
  <rights type="text" />
  <updated>2012-08-07T18:29:09Z</updated>
  <entry>
    <id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Composite?Query='keyword'&amp;$skip=0&amp;$top=1</id>
    <title type="text">ExpandableSearchResult</title>
    <updated>2012-08-07T18:29:09Z</updated>
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Web" type="application/atom+xml;type=feed" title="Web" href="ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web">
      <m:inline>
        <feed xmlns:base="https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
          <title type="text">Web</title>
          <subtitle type="text">Bing Search API</subtitle>
          <id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web</id>
          <rights type="text"></rights>
          <updated>2012-08-07T18:29:09Z</updated>
          <link rel="next" href="https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='keyword'&amp;$skip=3&amp;$top=50" />
          <entry>
            <id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web?$skip=1&amp;$top=1</id>
            <title type="text">WebResult</title>
            <updated>2012-08-07T18:29:09Z</updated>
            <content type="application/xml">
              <m:properties>
                <d:ID m:type="Edm.Guid">a6c62628-0012-42a2-b65c-513c82f523d1</d:ID>
                <d:Title m:type="Edm.String">title ...</d:Title>
                <d:Description m:type="Edm.String">title: ...</d:Description>
                <d:DisplayUrl m:type="Edm.String">sss.newikis.com/dd.html</d:DisplayUrl>
                <d:Url m:type="Edm.String">http://ss.newikis.com/ss.html</d:Url>
              </m:properties>
            </content>
          </entry>
        </feed>
      </m:inline>
    </link>
    <content type="application/xml">
      <m:properties>
        <d:ID m:type="Edm.Guid">3947df4e-b3b3-4be7-b25b-77852c8d312a</d:ID>
        <d:WebTotal m:type="Edm.Int64">3</d:WebTotal>
        <d:WebOffset m:type="Edm.Int64">0</d:WebOffset>
        <d:AlteredQuery m:type="Edm.String"></d:AlteredQuery>
        <d:AlterationOverrideQuery m:type="Edm.String"></d:AlterationOverrideQuery>
      </m:properties>
    </content>
  </entry>
</feed>

任何人都可以帮助我使用带有上述代码的PHP解决此问题?

你们非常接近,只错过了几个小(但关键)点。

所需的更改最少

您只需更改原始代码的三行即可。

  1. 获取<entry>元素。

    $feeds = $xml->entry->link->children('m', TRUE)->inline->entry;
    

    成为

    $feeds = $xml->entry->link->children('m', TRUE)->inline->children('', TRUE)->feed->entry;
    
  2. <m:properties>获取<d:*>元素。

    $result = $data->children('m', TRUE)->properties->children('d', TRUE);
    

    成为

    $result = $results->content->children('m', TRUE)->properties->children('d', TRUE);
    
  3. 删除以下行。

    echo $data=(string)$results->content;
    

通过这些更改,脚本将提供所需的结果数组。

代码清理

下面的示例是对原始内容的一个小修改,它以稍微整洁*的方式从提要构建$search数组。

*在我看来:读者可能不同意。艰难,这是我的答案。

$xml = new SimpleXMLElement($rs);
$i = 0;
$search = array();
$entries = $xml->entry->link->children('m', TRUE)->inline->children('', TRUE)->feed->entry;
foreach ($entries as $entry) {
    $i++;
    $properties = $entry->content->children('m', TRUE)->properties->children('d', TRUE);
    $url = (string) $properties->Url;
    $url = str_replace("&", "&amp;", urldecode($url));
    $search[$i] = array(
        'url'      => $url,
        'abstract' => (string) $properties->Description,
        'title'    => (string) $properties->Title,
        'rank'     => $i,
    );
}

发生了什么变化?

  1. 这是重要的一点:如"最小更改"中所述,$entries行使用 children('', TRUE)->feed 访问<m:inline>中的<feed>元素。
  2. 在适当的情况下,变量名称略有更改,以反映变量所表示的 XML 元素的名称。我发现这在使用SimpleXML时有很大帮助。
  3. 一次将所有内容分配给$search[$i];这只是个人喜好,因为我认为它看起来更整洁。
  4. 摆脱了那些奇怪的混合echo和分配线。

不确定这是否是你问题的答案,但这些位看起来很可疑:

echo $data=(string)$results->content;

$data现在包含一个字符串,因为您在将 $results->content 的值强制转换为字符串之前将其分配给 $data 。但代码中的下一行是:

$result = $data->children('m', TRUE)->properties->children('d', TRUE);

在这里,$data显然应该是一个对象而不是一个字符串。

如果你需要那里的回声,把线分开:

$data = $results->content;
echo $data; // casting to string is done automatically

最新更新