我对XML几乎一无所知。这个脚本是为我们写的......
我有一个脚本从数据库中提取信息并创建一个XML文件。 如果pull字段为空/ null,则将其格式化为:
<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();
?>
答案 0 :(得分:4)
您可以选择几个选项。
没有空元素。
$result->addChild('deal'); // <deal/>
$result->addChild('deal', ''); // <deal></deal>
使用不适用于SimpleXML的LIBXML_NOEMPTYTAG
(docs)(但您可以轻松地将DOM与SimpleXML对象一起使用)。
$doc = dom_import_simplexml($result)->ownerDocument;
echo $doc->saveXML(NULL, LIBXML_NOEMPTYTAG);
答案 1 :(得分:1)
首先,所有这些陈述都是错误的:
$result->addChild('id', $store_deal_details[id]);
正确的语法是:
$result->addChild('id', $store_deal_details[ 'id' ]);
关于你的问题,请通过,例如的&#39; &#39;因为addChild()的第二个参数应该创建你想要的东西。
另外,这个
$result = new SimpleXMLElement( $xml_root, LIBXML_NOEMPTYTAG );
可以做这个伎俩。文档解释了这个选项:
This option is currently just available in the DOMDocument::save
and DOMDocument::saveXML functions.
答案 2 :(得分:0)
请参阅http://www.php.net/manual/en/libxml.constants.php
SimpleXMLElement构造函数将选项作为参数。您可以指定LIBXML_NOEMPTYTAG
,但文档指定它仅对DOMDocument::save
和DOMDocument::saveXML
答案 3 :(得分:0)
要求详细的XML标记有许多可能的原因。一个非常常见的情况是当您使用PHP生成属性列表以在iOS应用程序中使用时。我遇到了最初问到的问题。
对于plists,看起来最好的解决方案是完全消除标签,以及随附的<key>
元素。例如,以下所有内容都导致xcode中出现“损坏的plist”错误:
<key>someNumber</key>
<integer />
<key>someNumber</key>
<key>someNumber</key>
<integer></integer>
<key>someNumber</key>
<integer> </integer>
<key>someNumber</key>
<integer>NULL</integer>
plist规范允许的唯一内容是完全消除这两个元素。幸运的是,在Objective-C中,当检索plist中不存在的键的objectForKey
值时,结果只是一个空值,而不是错误。因此很容易在客户端捕获这种情况。
只是把它放在那里以防万一像我这样的人(或未来的我)遇到同样的情况。