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();
?>
I know almost nothing about XML. This script was written for us...
I have a script that pulls info from the database and creates an XML file.
If a pulled field is empty/null, it is formatting it like so:
<deal_id/>
I know that this is valid, but I need it formatted like so:
<deal_id></deal_id>
Here is the script in question.
<?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();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以选择几个选项。
不要有空元素。
使用
LIBXML_NOEMPTYTAG
(docs),它不适用于 SimpleXML(但将 DOM 与 SimpleXML 对象一起使用很容易)。You have the choice of a few options.
Don't have empty elements.
Use
LIBXML_NOEMPTYTAG
(docs) which is not available for SimpleXML (but it is easy to use the DOM with your SimpleXML object).首先,所有这些语句都是错误的:
正确的语法是这样的:
关于您的问题,将 ' ' 作为 addChild() 的第二个参数传递应该会创建您想要的内容。
此外,这
可能可以达到目的。该文档对此选项进行了解释:
First, all those statements are errors:
Correct syntax is this:
Regarding your question, passing e.g. ' ' as the second parameter of addChild() should create what you want.
Additionally, this
may do the trick. The documentation explains this regarding the option:
请参阅 http://www.php.net/manual/en/libxml.constants。 php
SimpleXMLElement 构造函数将选项作为参数。您可以指定
LIBXML_NOEMPTYTAG
,但文档指定它仅对DOMDocument::save
和DOMDocument::saveXML
有效See http://www.php.net/manual/en/libxml.constants.php
The SimpleXMLElement constructor takes options as parameters. You can specify
LIBXML_NOEMPTYTAG
, though the docs specify that it's only valid forDOMDocument::save
andDOMDocument::saveXML
需要详细 XML 标记的可能原因有很多很多。一种非常常见的场景是当您使用 PHP 生成在 iOS 应用程序中使用的属性列表时。我遇到了最初在这里提出的问题。
对于 plist,看起来最好的解决方案是完全消除标签及其附带的
元素。例如,以下所有情况都会导致 xcode 中出现“损坏的 plist”错误:plist 规范允许的唯一事情是完全消除这两个元素。幸运的是,在 Objective-C 中,当检索 plist 中不存在的键的
objectForKey
值时,结果只是一个空值,而不是错误。所以很容易在客户端陷入这种情况。只是把这个放在那里以防像我这样的其他人(或未来的我)遇到同样的情况。
There are many, many possible reasons to require verbose XML tags. One really common scenario is when you're using PHP to generate a property list for use in an iOS app. I ran into exactly the problem originally asked here.
For plists, it looks like the best solution is to eliminate the tag altogether, as well as its accompanying
<key>
element. For example, all of the following resulted in a "corrupted plist" error in xcode:The only thing that the plist spec allowed was to eliminate the two elements entirely. Fortunately, in Objective-C, when retrieving the
objectForKey
value for a key that doesn't exist in the plist, the result is simply a null value, not an error. So it was easy to trap this condition on the client side.Just putting this out there in case someone else like me (or a future me) runs into the same situation.