Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning xml instead of html 1

Status
Not open for further replies.

yahoo182

Programmer
Jul 5, 2005
70
CA
Hi there,
I am trying to return a raw xml file instead of HTMl from a php file and display this raw XML into a browser.
Does anyone know how to do this?

Thanks :)
 
Yes. I know how to do it.

What have you tried? Post some code and we can help you.

Ken
 
Hi there,
This is what I have so far.

Code:
<html>
<head>
<title>get hotel XML
</title>
</head>


<body>

<?php
$dbcnx = @mysql_connect('localhost', 'username', '*****');
if (!$dbcnx) {
  die( '<p>Unable to connect to the database server at this time.</p>' );
}

// Select the jokes database
if (! @mysql_select_db('hotelInfo') ) {
  die( '<p>Unable to locate the grus database at this time.</p>' );
}

$id=$_GET['id'];

// get description info from description table in db
$descript = @mysql_query("select * from description where HotelID=$id");

if (!$descript) {
  die('<p>Error performing query: ' . mysql_error() .'</p>');
}

while ( $row = mysql_fetch_array($descript) ) {
echo ('<?xml version="1.0" encoding="ISO-8859-1"?>')
echo ('<hotels>');
echo ('<hotel>');
echo ('<hotelID>'.$row['HotelID'].'<hotelID>');
echo ('<MarketingLevel>'.$row['MarketingLevel'].'</MarketingLevel>');
echo ('<PropertyDescription>'.$row['PropertyDescription'].'</PropertyDescription>');
echo ('</hotels>');
echo ('</hotel>');
}
?>
 
I think what I need to do is to change the content-type of the php return to
"Content-type: text/xml\n\n"
instead of
"Content-type: text/html\n\n"

But just simply typing that at the top of the file does not seem to work .
 
You need to send header before you start sending the data:

Code:
[red]header('Content-Type: text/xml');[/red]
while ($row = mysql_fetch_[red]assoc[/red]($descript)) {
    echo '<?xml version="1.0" encoding="ISO-8859-1"?>'[red];[/red]
    echo '<hotels>';
    echo '<hotel>';
    echo '<hotelID>'.$row['HotelID'].'<hotelID>';
    echo '<MarketingLevel>'.$row['MarketingLevel'].'</MarketingLevel>';
    echo '<PropertyDescription>'.$row['PropertyDescription'].'</PropertyDescription>';
    echo '</hotels>';
    echo '</hotel>';
}

My additions/changes are in [red]red[/red].

Also you don't need the parenthises ( ) in your echo statements.

Ken
 
Thanks Ken,
That helps a lot! :)
is there a difference between mysql_fetch_assoc and mysql_fetch_array?
 
mysql_fetch_assoc will return an associatve array while mysql_fetch_array returns an array which contains both number and associative indicies.

see for more information.

Ken
 
Just as a note:

I've been lately using the DOM XML functions or the newer DOM functions to generate XML documents by attaching nodes rather than generating text output wrapped in tags.
It is a great alternative and makes sure the syntax will always be ok.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top