将php函数输出到xml


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至英镑

 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)

我建议切换到(例如)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>&lt;img /&gt;</amnt>
  </to>
</conv>

看看最后一个<amnt />,它是正确的"xml转义",您不必再担心它了,但在使用DOMELement->noveValue = htmlAFAIK时要小心,这不会转义您的值。

当然,不要忘记设置正确的header(),使用(在发送任何输出之前):

header('Content-Type: text/xml, charset=utf-8');

还可以看看维基上的14.17内容类型和MIME类型。

htmlspecialchar()很可能是你的朋友,就像在中一样

echo '<loc>' . htmlspecialchars($locFrom) . '</loc>';

此外,正如ZiTAL所提到的,任何输出之前的header('Content-Type: text/xml, charset=utf-8')都是必须的。