我想从Google Suggestions XML中读取XML标记的属性值。我使用的技术是PHP和SimpleXML。我也可以使用JavaScript。 这是Google建议XML http://clients1.google.com/complete/search?hl=en&output=toolbar&q=computer 要么 http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=Computer
XML文件的格式:
<toplevel>
<CompleteSuggestion>
<suggestion data="computershare"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="computer desk"/>
</CompleteSuggestion>
<CompleteSuggestion>
<suggestion data="computer science"/>
</CompleteSuggestion>
</toplevel>
我正在使用此代码来读取XML文件:
<?php
$xml=simplexml_load_file("http://clients1.google.com/complete/search?hl=en&output=toolbar&q=computer");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
输出:
顶层
CompleteSuggestion:
CompleteSuggestion:
CompleteSuggestion:
CompleteSuggestion:
CompleteSuggestion:
CompleteSuggestion:
我从未使用过XML文件,因此不知道如何读取XML标记的ATTRIBUTES值。即
Computershare的
电脑桌
计算机科学
等,在上面的XML文件中。
请帮帮我。
答案 0 :(得分:1)
这应该有效:
foreach($xml->children() as $child)
{
foreach($child->suggestion[0]->attributes() as $data);
echo $child->getName() . ": " . $data . "<br>";
}
答案 1 :(得分:0)
访问元素的属性,就像访问元素一样 阵列。
foreach($xml->children() as $child){
echo $child->getName() . ": " . $child . ($child['data']? " | data: " . $child['data'] : "") . "<br>";
}