从PHP中提取XML

时间:2015-11-24 20:23:57

标签: php xml

<toplevel>
 <CompleteSuggestion>
  <suggestion data="shook ones part 2"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
  <suggestion data="shook me all night long"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
  <suggestion data="shook ones instrumental"/>
 </CompleteSuggestion>
 <CompleteSuggestion>
  <suggestion data="shook me all night long lyrics"/>
 </CompleteSuggestion>
</toplevel>

我想从这个XML输出中提取“shook ones part 2”。我已经完成了所有解决方案,但我无法做到这一点。 我试过这个:

<?php
$url = 'http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=shook&ds=yt';
$exact = simplexml_load_file($url);
echo $exact->CompleteSuggestion[0]->suggestion;
?>

3 个答案:

答案 0 :(得分:1)

shook ones part 2data的属性suggestion的值。 因此,您应该获得suggestion的所有属性attributes()并选择data

$exact = simplexml_load_file('$url');
echo $exact->CompleteSuggestion[0]->suggestion->attributes()->data;

答案 1 :(得分:0)

忽略

中的单引号
simplexml_load_file('$url');
                    ^    ^

答案 2 :(得分:0)

您可以使用DOMDocumen t。下面的示例将搜索标记建议并回显数据属性:

  private static object OnCoerceValue(DependencyObject d, object baseValue)
    {  
        if (((DataGrid)d).HasItems)
        {
            DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
            if(row !=null)
            { 
                if ((bool)baseValue)
                {
                    FocusManager.SetIsFocusScope(row, true);
                    FocusManager.SetFocusedElement(row, row);
                }
                else if (((UIElement)d).IsFocused)
                    Keyboard.ClearFocus();
            }
        }
        return ((bool)baseValue);            
    }
相关问题