简体   繁体   中英

outputting a php function to 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)

htmlspecialchars() is quite probably your friend, as in

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

Also, as mentioned by ZiTAL, header('Content-Type: text/xml, charset=utf-8') before any output is a must.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM