Using a Controller to Display XML

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a Controller to Display XML

Salvador Ledezma

Hello,

I have a small application that uses the controller/view classes of the framework and I am having trouble displaying XML data.

I have a FeedController class and in that class I have a listAction() function.  The function is quite simple.  I am storing some feeds and just call a getter function to retrieve a DOM representation of the feeds.  Then I display the XML.  Eventually, I'll use the View to render this output in another file, but for now I just wanted to test it.  (It turns out that even rendering it in another file does not work).

This is the function:

public function listAction()
{
        $fdb = Zend::registry('fdb');
        $dom = $fdb->getFeedsDom();
       
        header("Content-type: text/xml");
        echo $dom->saveXML();
}

I am trying to display the raw XML using a text/xml content-type and the url: //localhost/feed/list.

My problem is that I get the following error:

XML Parsing Error: xml declaration not at start of external entity
Location: http://localhost/feed/list
Line Number 2, Column 1:<?xml version="1.0"?>
^

It seems some extra whitespace is being inserted before the XML is output.  Maybe the controller is doing this somehow?  Is there a way we can control the full page or to flush out any unnecessary output?

If I am doing something wrong, are there any suggestions to help me figure out the best way to output raw XML within the controller architecture?

Thanks,

-Salvador.
Reply | Threaded
Open this post in threaded view
|

Re: Using a Controller to Display XML

Michael Patrick-3
Salvador Ledezma wrote:

> XML Parsing Error: xml declaration not at start of external entity
> Location: http://localhost/feed/list
> Line Number 2, Column 1:<?xml version="1.0"?>
> ^
>
> It seems some extra whitespace is being inserted before the XML is output.
>  Maybe the controller is doing this somehow?  Is there a way we can
> control the full page or to flush out any unnecessary output?
>
> If I am doing something wrong, are there any suggestions to help me figure
> out the best way to output raw XML within the controller architecture?

Make sure that none of your include/require files have random whitespace
at the end of them after a ?>

In my very simple test I was able to echo raw XML from a controller fine
( but all my include files leave off the ?>  to prevent random whitespace ).

        function xmlAction() {
                header('Content-Type: text/xml');
                $xml = simplexml_load_file(VERTICAL_MENU_XML);
                echo $xml->asXML();
        }

The VERTICAL_MENU_XML file simply pointed to a valid XML file on disk.

Michael
Reply | Threaded
Open this post in threaded view
|

RE: Using a Controller to Display XML

Nayana Hettiarachchi - GMI
In reply to this post by Salvador Ledezma
From my past experience if you had a white space prior to a header()
function wont you usually get an error saying the output already started
else where ?

Hard to say without seeing the code but your best bet is to check for
white spaces after the content type is established and before the xml
output begins.

Regards,
Nayana




-----Original Message-----
From: Michael [mailto:[hidden email]]
Sent: Thursday, May 25, 2006 7:17 PM
To: [hidden email]
Subject: Re: [fw-general] Using a Controller to Display XML

Salvador Ledezma wrote:
> XML Parsing Error: xml declaration not at start of external entity
> Location: http://localhost/feed/list
> Line Number 2, Column 1:<?xml version="1.0"?>
> ^
>
> It seems some extra whitespace is being inserted before the XML is
output.
>  Maybe the controller is doing this somehow?  Is there a way we can
> control the full page or to flush out any unnecessary output?
>
> If I am doing something wrong, are there any suggestions to help me
figure
> out the best way to output raw XML within the controller architecture?

Make sure that none of your include/require files have random whitespace

at the end of them after a ?>

In my very simple test I was able to echo raw XML from a controller fine

( but all my include files leave off the ?>  to prevent random
whitespace ).

        function xmlAction() {
                header('Content-Type: text/xml');
                $xml = simplexml_load_file(VERTICAL_MENU_XML);
                echo $xml->asXML();
        }

The VERTICAL_MENU_XML file simply pointed to a valid XML file on disk.

Michael
Reply | Threaded
Open this post in threaded view
|

Re: Using a Controller to Display XML

Michael Patrick-3
Nayana Hettiarachchi wrote:
> From my past experience if you had a white space prior to a header()
> function wont you usually get an error saying the output already started
> else where ?

I changed my test to

        function xmlAction() {
                echo " ";
                header('Content-Type: text/xml');
                $xml = simplexml_load_file(VERTICAL_MENU_XML);
                echo $xml->asXML();
        }

and I get the XML error like Salvador but NOT a PHP error with header
whining about output before a header.  After I read your comment I
thought you were right about header griping but I guess not.  So, he
could still have random whitespace further "up" in the file stack.

Michael
Reply | Threaded
Open this post in threaded view
|

RE: Using a Controller to Display XML

Nayana Hettiarachchi - GMI
In reply to this post by Salvador Ledezma

Micheal,

Thanks for pointing this out, I guess it may have to do with the way the
Zend_Controler handles ob, because this piece of code

        echo " ";
        header('Content-Type: text/xml');
        echo "<?xml version=\"1.0\"><test />";

Gives me " Warning: Cannot modify header information - headers already
sent by (output started at /var/www/hbdev/xml.php:2) in
/var/www/hbdev/xml.php on line 3"

Can someone explain if there are special characteristics to the way
output buffer is handled in the Zend Framework?

Regards,
Nayana


-----Original Message-----
From: Michael [mailto:[hidden email]]
Sent: Thursday, May 25, 2006 7:34 PM
To: [hidden email]
Subject: Re: [fw-general] Using a Controller to Display XML

Nayana Hettiarachchi wrote:
> From my past experience if you had a white space prior to a header()
> function wont you usually get an error saying the output already
started
> else where ?

I changed my test to

        function xmlAction() {
                echo " ";
                header('Content-Type: text/xml');
                $xml = simplexml_load_file(VERTICAL_MENU_XML);
                echo $xml->asXML();
        }

and I get the XML error like Salvador but NOT a PHP error with header
whining about output before a header.  After I read your comment I
thought you were right about header griping but I guess not.  So, he
could still have random whitespace further "up" in the file stack.

Michael
Reply | Threaded
Open this post in threaded view
|

RE: Using a Controller to Display XML

Nayana Hettiarachchi - GMI
In reply to this post by Salvador Ledezma

I realized that depending on how your error_reporting is setup, you may
or may not see that warning,


Micheal,

Thanks for pointing this out, I guess it may have to do with the way the
Zend_Controler handles ob, because this piece of code

        echo " ";
        header('Content-Type: text/xml');
        echo "<?xml version=\"1.0\"><test />";

Gives me " Warning: Cannot modify header information - headers already
sent by (output started at /var/www/hbdev/xml.php:2) in
/var/www/hbdev/xml.php on line 3"

Can someone explain if there are special characteristics to the way
output buffer is handled in the Zend Framework?

Regards,
Nayana

Nayana Hettiarachchi wrote:
> From my past experience if you had a white space prior to a header()
> function wont you usually get an error saying the output already
started
> else where ?

I changed my test to

        function xmlAction() {
                echo " ";
                header('Content-Type: text/xml');
                $xml = simplexml_load_file(VERTICAL_MENU_XML);
                echo $xml->asXML();
        }

and I get the XML error like Salvador but NOT a PHP error with header
whining about output before a header.  After I read your comment I
thought you were right about header griping but I guess not.  So, he
could still have random whitespace further "up" in the file stack.

Michael
Reply | Threaded
Open this post in threaded view
|

Re: Using a Controller to Display XML

Salvador Ledezma
In reply to this post by Salvador Ledezma

Michael/Nayana,

Thank you for your help.  I followed your suggestions and removed all the ?> from my classes and tried as much as possible to remove whitespace, but it still didn't work.  I went back and staring at the error message, I noticed that it pointed to line number 2:

XML Parsing Error: xml declaration not at start of external entity
Location: http://localhost/feed/list

Line Number 2, Column 1:<?xml version="1.0"?>
^


So somewhere in the code, there was an extra line feed or carriage return causing the output to start on the 2nd line.  Finally out of desperation, I cleaned out the buffer and this worked!  I was able to output XML.

ob_end_clean();
header("Content-type: text/xml");
echo $dom->saveXML();

Seems like a hack, but at least I can now move forward.

Thanks for the help,

-Salvador.


>Michael wrote:
To:
[hidden email]
Subject:
Re: [fw-general] Using a Controller to Display XML

Salvador Ledezma wrote:
> XML Parsing Error: xml declaration not at start of external entity
> Location: http://localhost/feed/list
> Line Number 2, Column 1:<?xml version="1.0"?>
> ^
>
> It seems some extra whitespace is being inserted before the XML is output.
>  Maybe the controller is doing this somehow?  Is there a way we can
> control the full page or to flush out any unnecessary output?
>
> If I am doing something wrong, are there any suggestions to help me figure
> out the best way to output raw XML within the controller architecture?

Make sure that none of your include/require files have random whitespace
at the end of them after a ?>

In my very simple test I was able to echo raw XML from a controller fine
( but all my include files leave off the ?>  to prevent random whitespace ).

                function xmlAction() {
                                 header('Content-Type: text/xml');
                                 $xml = simplexml_load_file(VERTICAL_MENU_XML);
                                 echo $xml->asXML();
                }

The VERTICAL_MENU_XML file simply pointed to a valid XML file on disk.

Michael