Current location: Hot Scripts Forums » Programming Languages » PHP » How to referene perticuler xml data in php?


How to referene perticuler xml data in php?

Reply
  #1 (permalink)  
Old 02-20-07, 04:51 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy How to referene perticuler xml data in php?

How to referene perticuler xml data in php?

Hi all. i have an xml file as shown below and i want to reference specific data out it.(The data shown in bold)Could any one show me how to reference those data so i can use them later as variables.I not only want to output them all at once.I also want to learn how to reference individual items separately because i have another part that uses those 3 variables(Artistname,songname,songimage).i be happy if some show me how this can be done in php.Thanks


xml file(rss feed with one set of data always):
Code:
<playing>
<artist>Cindy</artist> 
<song>echo</song> 
<image>http://www.somesite.com/song_images/cindy.jpg</image>
<rating>3.5</rating>
<songid>4736</songid>
</playing>
php code that uses retrevied data:

Code:
<?
$imageurl = "http://www.somesite.com/song_images/cindy.jpg";
$artist = "cindy";
$song = "echo";
.....
.....

?>
Reply With Quote
  #2 (permalink)  
Old 02-20-07, 05:10 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
This should do it.
PHP Code:

<?php

$xml 
file_get_contents('your-xml-file.xml');

preg_match_all('/<(artist|song|image)>([^<]+)<\/\\1>/i'$xml$matches);

list(
$artist$song$image) = $matches[2];

echo 
$artist;
// ...

?>
Reply With Quote
  #3 (permalink)  
Old 02-21-07, 07:18 AM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Nico thanks for u reply. But when i run your code i don't get any thing printed on screen. i used echo to test before using those artist ,song and image varible in my remaining program. could tell me what is wrong. The xml is in a remote server.Thanks
Reply With Quote
  #4 (permalink)  
Old 02-21-07, 07:26 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I've tested the code locally and it worked for me. The text between the tags will be stored in these variables: $artist, $song, $image. So if you echo them it should display the right content.

Do you get any errors, like the file could not be opened or something? Also, in the XML file is always one set only, as you said, right?

Try echoing $xml too, to see if the XML is alright.
Reply With Quote
  #5 (permalink)  
Old 02-21-07, 07:52 AM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
I've tested the code locally and it worked for me. The text between the tags will be stored in these variables: $artist, $song, $image. So if you echo them it should display the right content.

Do you get any errors, like the file could not be opened or something? Also, in the XML file is always one set only, as you said, right?

Try echoing $xml too, to see if the XML is alright.
Nico many thanks i tested it worked so good. The problem was with xml file. It need to be called like localhost/rss.php?xml=true and i was using rss.php only!!

Nico i did a lot of search on web most of the tutorial they talk about outputing the whole xml but they never show how to refrence indvidual items in xml and thanks to you that help me learn this.
Reply With Quote
  #6 (permalink)  
Old 02-21-07, 08:12 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
This method of parsing XML is only good if there's only one set, like in your case. If there are more, I'd use preg_match_all() to get all items, then I'd loop through them and parse the data in each item. Otherwise it could conflict.
Reply With Quote
  #7 (permalink)  
Old 02-21-07, 10:00 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Nico i get in to this problem. When the imageurl has space in image name i get garbage in php. I be happy if u look at it help me fix it.Thanks



Code:
<br />
<b>Warning</b>:  imagecreatefromjpeg(./david bob.jpg): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in <b>/home/www/script.php</b> on line <b>27</b><br />
<br />
<b>Warning</b>:  imagecopymerge(): supplied argument is not a valid Image resource in <b>/home/www/script.php</b> on line <b>28</b><br />
PNG


IHDRB IDATxy|]Wu?]^I5Y[`Y;s8	$!!$_)B/)Ёh?@[t miZ$JN!vl2cϓdk5[gr~Zs

......
.....



Code:
<?php


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

  	$background = imagecreatefrompng("./backround_image.png");

$xml = file_get_contents("./rss.php?xml=true");

preg_match_all('/<(artist|song|image)>([^<]+)<\/\\1>/i', $xml, $matches);

list($artist, $song, $imageurl) = $matches[2];

//echo $artist;
//echo $song;
//echo $imageurl;



  	$insert = imagecreatefromjpeg($imageurl);
  	imagecopymerge($background,$insert,47,50,0,0,80,80,100);

......
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving data from XML file dodotopia JavaScript 5 10-26-06 10:03 AM
Split data in php from sql? ( usinf | ) ? NabZ PHP 1 04-03-06 03:51 PM
How to get PHP to input data into a MYSQL table? scl789 PHP 5 04-21-05 09:06 PM
one-page form/catalog in php, data fed in with .csv file? domaky PHP 2 10-25-03 09:32 AM
help plz: format retrieved Mysql data in HTML with PHP paulj000 PHP 2 10-19-03 08:03 PM


All times are GMT -5. The time now is 05:56 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.