我對XML幾乎一無所知。這個腳本是爲我們寫的...XML - 顯示打開和關閉而不是空元素
我有一個腳本,從數據庫中提取信息並創建一個XML文件。 如果拉字段爲空/空,它正在格式化它像這樣:
<deal_id/>
我知道,這是有效的,但我需要它格式化,像這樣:
<deal_id></deal_id>
這裏是腳本有問題。
<?php
//include global config
require("config.inc.php");
//include MySQL wrapper
require("Database.singleton.php");
// instantiate database
$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
// define basic xml data
$xml_root = "<?xml version='1.0' encoding='UTF-8' ?>\n"."<store></store>";
$deal_id = $_REQUEST['deal_id'];
$result = new SimpleXMLElement($xml_root);
$img_path = "http://www.malldeals.com/admin/images/logos/deals/";
$img_na = "image_na.jpg";
if ($deal_id != '') {
$db->connect();
// get store and deal details
$sql_store_deal = "SELECT businesses.id, businesses.business_name, businesses.business_floor, businesses.business_near, businesses.business_phone, businesses.business_address,
businesses.business_city, businesses.latitude, businesses.longitude, deals.title AS deal_title, deals.id AS deal_id, deals.percent_off AS deal_discount, deals.sale_price AS deal_price,
deals.orig_price AS deal_orig_price, deals.expiry_date AS deal_expires, deals.description AS deal_text,
(SELECT file_name
FROM images
WHERE images.deal_id = deals.id AND size_key = '1') AS thumb2,
(SELECT COUNT(poster_id)
FROM deals
WHERE poster_id=businesses.id) AS num_deals
FROM businesses, deals
WHERE deals.poster_id=businesses.id
AND deals.id='".$db->escape($deal_id)."'";
//
$store_deal_details = $db->query_first($sql_store_deal);
$store_logo_url = ($store_deal_details[thumb2] != "") ? ($img_path.$store_deal_details[thumb2]) : $img_path.$img_na;
$result->addChild('id', $store_deal_details[id]);
$result->addChild('name', utf8_encode($store_deal_details[business_name]));
$result->addChild('floor', utf8_encode($store_deal_details[business_floor]));
$result->addChild('near', utf8_encode($store_deal_details[business_near]));
$result->addChild('phone', utf8_encode($store_deal_details[business_phone]));
$result->addChild('latitude', $store_deal_details[latitude]);
$result->addChild('longitude', $store_deal_details[longitude]);
$result->addChild('address', utf8_encode($store_deal_details[business_address]));
$result->addChild('city', utf8_encode($store_deal_details[business_city]));
$result->addChild('num_deals', $store_deal_details[num_deals]);
$deal_discount = floatval($store_deal_details[deal_discount]);
$deal_price = floatval($store_deal_details[deal_price]);
$deal_savings = floatval($store_deal_details[deal_orig_price]) - floatval($store_deal_details[deal_price]);
$result_deal = $result->addChild('deal');
$result_deal->addChild('deal_id', $store_deal_details[deal_id]);
$result_deal->addChild('title', xml_encode($store_deal_details[deal_title]));
$result_deal->addChild('discount', $deal_discount);
$result_deal->addChild('price', $deal_price);
$result_deal->addChild('savings', $deal_savings);
$result_deal->addChild('expires', $store_deal_details[deal_expires]);
$result_deal->addChild('text', xml_encode($store_deal_details[deal_text]));
$result_deal->addChild('image', $store_logo_url);
$db->close();
}
else {
$result->addChild('error', "Missing required parameters");
}
header("Content-Type: text/xml");
echo $result->asXML();
?>
你爲什麼需要格式化? – BoltClock
需要格式化,以供第三方使用。 –