dtl19910708 2012-02-19 11:49
浏览 20
已采纳

输出php函数到xml。

I have a Function (within the class Database)

            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();
                  }

when I call the function

$foo = new Database($from,$to);
$hey = $foo->LocTo();
echo $hey;

or

 $foo = new Database($from,$to);
 echo $foo->LocTo();

It out puts correctly.

Im trying to put into XML and im getting an encoding error.

This is my 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>';

Does anyone know why I am getting an encoding error? I have checked the source code and it gets as far as the location.

here is an example of the location out put.

AED to 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)
  • 写回答

2条回答 默认 最新

  • doukui4836 2012-02-19 12:08
    关注

    I recommend switching to (for example) DOM so your code would look like this:

    <?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();
    

    This should make your XML always valid. The result would look like (filled just with string '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>
    

    Take a look at last <amnt /> it's correctly "xml escaped", you don't have to worry about it anymore, but be careful when using DOMELement->noveValue = html AFAIK this doesn't escape your values.

    And of course don't forget to set correct header(), use (before sending any output):

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

    Also take a look at 14.17 Content-Type and MIME types on wiki.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么