[英]outputting a php function to xml.
我有一个函数(在数据库类中)
function locTo (){//return the location of the to currency
$con = dbconnect(); //instantiate db connection
$locationTo= mysql_query ("SELECT location FROM Sheet1 where currency_code = '$this->to'", $con);
$lol = mysql_fetch_array($locationTo);
return $lol['location'];
mysql_close();
}
当我调用函数
$foo = new Database($from,$to);
$hey = $foo->LocTo();
echo $hey;
要么
$foo = new Database($from,$to);
echo $foo->LocTo();
它正确放置。
我试图放入XML并收到编码错误。
这是我的XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<conv>';
echo'<at>'.$timefrom.'</at>';
echo'<rate>'.$rate.'</rate>';
echo'<from>';
echo'<code>'.$from.'</code>';
echo'<curr>'.$currencyFrom.'</curr>';
echo'<loc>'.$locFrom.'</loc>';
echo'<amnt>'.$amount.'</amnt>';
echo'</from>';
echo'<to>';
echo'<code>'.$to.'</code>';
echo'<curr>'.$currencyTo.'</curr>';
echo'<loc>'.$locTo.'</loc>';
echo'<amnt>'.$convertedAmount.'</amnt>';
echo'</to>';
echo'</conv>';
有谁知道我为什么会出现编码错误? 我已经检查了源代码,它可以到达该位置。
这是输出位置的示例。
AED转换为GBP
United Arab Emirates
United Kingdom, Crown Dependencies (the Isle of Man and the Channel Islands), certain British Overseas Territories ( South Georgia and the South Sandwich Islands, British Antarctic Territory and British Indian Ocean Territory)
htmlspecialchars()很可能是您的朋友,例如
echo '<loc>' . htmlspecialchars($locFrom) . '</loc>';
另外,正如ZiTAL所提到的,必须在任何输出之前加上header('Content-Type: text/xml, charset=utf-8')
。
我建议切换到(例如) DOM,以便您的代码如下所示:
<?php
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true; // Just for presentation, don't use this in real app
$main = $dom->createElement( 'conv');
$dom->appendChild( $main);
// Generic attributes
$main->appendChild( $dom->createElement( 'at', $timefrom));
$main->appendChild( $dom->createElement( 'rate', $rate));
// Fill form
$from = $dom->createElement( 'from');
$from->appendChild( $dom->createElement( 'code', $row['from']));
$from->appendChild( $dom->createElement( 'curr', $currencyFrom));
$from->appendChild( $dom->createElement( 'loc', 'time'));
$from->appendChild( $dom->createElement( 'amnt', 'time'));
$main->appendChild( $from);
// Fill to
$to = $dom->createElement( 'to');
$to->appendChild( $dom->createElement( 'at', 'time'));
$to->appendChild( $dom->createElement( 'code', 'time'));
$to->appendChild( $dom->createElement( 'curr', 'time'));
$to->appendChild( $dom->createElement( 'loc', 'time'));
$to->appendChild( $dom->createElement( 'amnt', '<img />'));
$main->appendChild( $to);
echo $dom->saveXML();
这应该使您的XML始终有效。 结果看起来像(仅用字符串'time'
填充):
<?xml version="1.0" encoding="UTF-8"?>
<conv>
<at>time</at>
<rate>time</rate>
<from>
<code>time</code>
<curr>time</curr>
<loc>time</loc>
<amnt>time</amnt>
</from>
<to>
<at>time</at>
<code>time</code>
<curr>time</curr>
<loc>time</loc>
<amnt><img /></amnt>
</to>
</conv>
看看最后一个<amnt />
它正确地“转义了xml”,您不必再担心了,但是在使用DOMELement->noveValue = html
AFAIK时要小心,这不会逃避您的值。
当然,不要忘记设置正确的header()
,使用(在发送任何输出之前):
header('Content-Type: text/xml, charset=utf-8');
还可以查看Wiki上的14.17 Content-Type和MIME类型 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.