Welcome Guest, Not a member yet? Register   Sign In
xml version and encoding using dbutil
#1

[eluser]Pana_Ruplahlava[/eluser]
Hi,
i'm using this script from guide
Code:
$this->load->dbutil();

  $query = $this->db->query("SELECT id_person, name, url FROM person");
  $config = array (
   'root'    => 'root',
   'element' => 'person',
   'newline' => "\n",
   'tab'    => "\t"
   );

echo $this->dbutil->xml_from_result($query, $config);
$this->output->set_content_type('text/xml');
$this->output->set_output($xml);
Its really cool, whole xml on just a bunch of lines.
BUT! I need it to use it with a version and encoding tag.
Code:
<?xml version="1.0" encoding="UTF-8"?>
I could not find, if there is any way use this helper and set xml version and encoding. When i just try to echo it, it gives me an error: XML declaration not at beginning of document

Any ideas? I'm an xml beginner, so if there is a php function just for it, im sorry, i still could not find it Big Grin


PS. That not valid document with echoed xml version looks like this.

Code:
&lt;?xml version="1.0" encoding="UTF-8"?&gt;<root>
<person>
  <id_person>1</id_person>
  <name>Franta Brabec</name>
  <url>http://www.flixya.com/files-photo/s/a/p/sapnwnewbie-1943053.jpg</url>
</person>
<person>
  <id_person>2</id_person>
  <name>Personov Druhovič</name>
  <url>http://animal.discovery.com/guides/wild-birds/gallery/mallard_duck.jpg</url>
</person>
</root>

It starts with 3 blank lines with bunch of spaces(i did not put them there...)
#2

[eluser]TheFuzzy0ne[/eluser]
The XML returned from the helper function is a string.

Bearing that in mind, you should be able to set anything you need before sending the output to the browser.

Code:
$xml  = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;';
$xml .= $this->dbutil->xml_from_result($query, $config);

$this->output->set_content_type('text/xml');
$this->output->set_output($xml);

Hope this helps.
#3

[eluser]Pana_Ruplahlava[/eluser]
Thx Smile Mistake was on another place, at the end of document i had 2 blank lines, after closing ?&gt; tag.. after deleting them i have valid XML Smile
Still I have no idea, why they are processed before my php code...
#4

[eluser]TheFuzzy0ne[/eluser]
There's a very simple answer. CodeIgniter buffers your output, and sends it all to the browser at once, with the appropriate headers, at the end of controller execution. However, anything outside of the PHP tags is sent straight to the browser, and the headers are automatically sent, so when CodeIgniter tries to send the headers, it can't.

Because of this, you are encouraged to omit the closing tag at the end of a PHP file. The PHP parser will not mind at all. If you look at all of the CodeIgniter source files, you'll see that it's omited there, too. Smile

Instead, it's a good idea to simply add a comment to the end of your PHP files, just so you can be sure it's not been truncated. For example, at the end of the file ./application/core/MY_Loader.php, I have the following:
Code:
/* End of file MY_Loader.php */
/* Location: ./application/core/MY_Loader.php */
#5

[eluser]Pana_Ruplahlava[/eluser]
cool! it has always ripped my heart seeing that unclosed tag, and now i know why Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB