這有點令人困惑,因此請提前與我聯繫,謝謝。使用SimpleXML按功能讀取返回的XML
我正在使用Kohana PHP框架來開發應用程序。我有一個模型函數接受搜索參數,並且應該返回一個XML樣式的頁面。我需要使用SimpleXML通過控制器讀取它。任何想法如何做到這一點?
$o = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$o .= "<feed>\n";
$o .= "\t<search_phrase>$q</search_phrase>\n";
if(isset($entries)){
uasort($entries, 'compare_weight');
/**
* Build the xml data
*/
foreach($modules as $module){
$o .= "\t<search_location>$module</search_location>\n";
}
foreach($entries as $k=>$entry){
$o .= "\n\t<entry>\n";
$o .= "\t\t<title>$entry[title]</title>\n";
$o .= "\t\t<url>$entry[url]</url>\n";
$o .= "\t\t<weight>$entry[weight]</weight>\n";
$o .= "\t\t<module>$entry[module]</module>\n";
if($entry['owners']){
foreach($entry['owners'] as $owner){
$o .= "\t\t<owners>\n";
$o .= "\t\t\t<owner_id>$owner[owner_id]</owner_id>\n";
$o .= "\t\t\t<owner_name>$owner[owner_name]</owner_name>\n";
$o .= "\t\t\t<profile_link>$owner[profile_link]</profile_link>\n";
$o .= "\t\t</owners>\n";
}
}
$o .= "\t</entry>\n";
}
}else{
$o .= "\t<noresult>true</noresult>\n";
}
$o .= "</feed>\n";
return $o;
控制器功能是這樣的......這是最接近我能夠包裝我的頭如何做到這一點。
$return= $this->search->search($_GET);
$xml = new SimpleXMLElement($return);
die($xml);
它返回一個帶有44個空白行的空白文檔。任何方向都會有幫助。
我建議使用DOM文檔來構建你的XML,因爲它提供了一個健壯API。 – 2010-08-18 17:02:37