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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help outputing xml in correct format using php

Status
Not open for further replies.

shobert09

Programmer
Sep 26, 2007
11
NL
Hi all. i try to output xml data using the following php code but
when i run the php script i don't get the xml output in correct format.



Code:
<?
echo"  <playing>\n";
echo"   <artist>Kambiz</artist> \n";
echo"   <song>Shookolat</song> \n";
echo"   <image>[URL unfurl="true"]http://www.somesite.com/song_images/kambiz.jpg</image>[/URL] \n";
echo"   <rating>2.44444</rating> \n";
echo"   <songid>3003</songid> \n";
echo"   </playing>\n";

?>


I want the xml to be outputed as shown in this pic:


correct output format:
Code:
- <playing>
  <artist>Kambiz</artist> 
  <song>Shookolat</song> 
  <image>[URL unfurl="true"]http://www.somesite.com/static/artists/shakila.jpg</image>[/URL] 
  <rating>2.44444</rating> 
  <songid>3003</songid> 
  </playing>

2002932855008392615_rs.jpg



Not correct xml format:

Code:
Kambiz Shookolat [URL unfurl="true"]http://www.somesite.com/song_images/kambiz.jpg[/URL] 2.44444 3003

2000514845296588372_rs.jpg


could any one tell me how to output the xml data in the following format.Thanks
 
You should add a header at the top of it if you load it to a browser.
[tt]
<?php
header("Content-type: application/xhtml+xml");
//etc etc same as your original
?>
[/tt]
It needs to be told what to do to be "correct".
 
tsuij thanks for u reply.I tried it but now it tries to download a file instead of showing any thing!!!
 
to display in a browser use htmlspecialchars() on on the output

Code:
<?
ob_start();
echo"  <playing>\n";
echo"   <artist>Kambiz</artist> \n";
echo"   <song>Shookolat</song> \n";
echo"   <image>[URL unfurl="true"]http://www.somesite.com/song_images/kambiz.jpg</image>[/URL] \n";
echo"   <rating>2.44444</rating> \n";
echo"   <songid>3003</songid> \n";
echo"   </playing>\n";
$contents = ob_get_contents();
ob_end_clean();
echo htmlspecialchars($contents);
?>
 
>I tried it but now it tries to download a file instead of showing any thing!!!
On the client m/c, can you load a .xml file to the browser? I test it in ie/ff no problem at all. Opera/netscape use their default stylesheet. Hence the latter display slightly differently.
 
actually i added :
Code:
header("Content-Type: text/xml");
just before the :

Code:
echo"  <playing>\n";

and it worked fine and also added :

Code:
<?  
header("Cache-Control: no-cache, must-revalidate");
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  	header ("Content-type: image/png");
.....
.....

to avoid any possible cache.is there any diffrence ?
 
i suspect adding a png content type to an xml feed will not be helpful. browsers can not (generally) accept multi-part information in the same request.
 
I am perplexed as the real message of op's last post.

With the header "text/xml", I have no opinion, it is as good for me. Is the op meant "application/xhtml+xml" failed him and did not work?

With the header "text/xml", did op's mean it work as what he looked forward to for him? (see even this is not clear to me.)

For the other settings, whether there is something for me to look into too, depends on the exact meaning of the message op wanted to convey of the above... Or my mastery of this language (en) is really not enough. Or the message is made obscur deliberately?
 
tsuji

the other settings posted by the OP simply stop the browaser from reloading the content from a cached source (i'm sure someone as experienced as you knows this). the last header directive is incorrect - I guess the OP has found this out by now but not posted back.

i have not tested this but I suspect that text/xml is a more widely accepted content-type than application/xhtml+xml if you define the universe of browsers from IE5 <=.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top