这是我的代码。请帮我把这段代码缩短为更少的行。数据从数据库中提取并添加到XML响应中。
header ("content-type: application/xml");
$xml = new SimpleXMLElement('<config/>');
$xml_display = $xml->addChild('nas_sharing');
$count = $xml_display->addChild('auth_state', "1");
$count = $xml_display->addChild('count', count($videos));
foreach($videos as $video){
$fileName = str_replace(" ", "", basename($video['file_path']));
$xml_item = $xml_display->addChild('item');
$xml_item->addChild('favorite', $video["album_id"]);
$xml_item->addChild('file_id', $video["file_id"]);
$xml_item->addChild('file_name', base64_encode($video["file_name"]));
$xml_item->addChild('file_ext', $video["file_ext"]);
$xml_item->addChild('media_type', $video["media_type"]);
$xml_item->addChild('file_size', $video["file_size"]);
$xml_item->addChild('file_ctime', $video["file_ctime"]);
$xml_item->addChild('file_owner', $video["file_owner"]);
$xml_item->addChild('comment', $video["comment"]);
$xml_item->addChild('inode', $video["inode"]);
$xml_item->addChild('file_path', base64_encode($video["file_path"]));
$xml_item->addChild('file_parsed', $video["file_parsed"]);
$xml_item->addChild('file_thumbed', $video["file_thumbed"]);
$xml_item->addChild('ctime_datetime', $video["ctime_datetime"]);
//$xml_item->addChild('album_file_id', $video["album_file_id"]);
//$xml_item->addChild('album_id', $video["album_id"]);
$xml_item->addChild('thumb_path', base64_encode(dirname($video['file_path']).'/.thumbnail/'.$fileName.'.jpg'));
}
if (count($videos) > 0) {
header ("content-type: application/xml");
$xml_stat = new SimpleXMLElement('<myvideo/>');
$xml_display = $xml_stat->addChild('status', "ok");
} else {
header ("content-type: application/xml");
//$app->response->setStatus(200);
$xml_stat = new SimpleXMLElement('<myvideo/>');
$xml_display = $xml_stat->addChild('status', "not found");
}
echo $xml_stat->asXml();
$xml->asXml('../xml/myvideo_search_allvideo_'.$user.'.xml');
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
答案 0 :(得分:0)
您可以替换(几乎)所有行,例如
$xml_item->addChild('file_id', $video["file_id"]);
foreach
构造:
foreach($video as $key => $value) {
$xml_item->addChild($key, $value);
}
您需要注意一些特殊情况,例如重命名:
$xml_item->addChild('favorite', $video["album_id"]);
和转换:
$xml_item->addChild('file_path', base64_encode($video["file_path"]));