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!

How to referene perticuler xml data in php?

Status
Not open for further replies.

DavidPlus

Programmer
Feb 20, 2007
38
NL
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

Code:
<playing>
<artist>[b]Cindy[/b]</artist> 
<song>[b]echo[/b]</song> 
<image>[b][URL unfurl="true"]http://www.somesite.com/song_images/cindy.jpg[/URL][/b]</image>
<rating>3.5</rating>
<songid>4736</songid>
</playing>

php code that uses retrevied data:

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

?>
 
jpadie thanks for u reply. that xml is a feed that updates every 5 min.I have some scripts that outputs the whole data but i just want those 3 data as variables so i can use and refrence them in my php project. As u see most of example are outputing the whole data.There is one example uses array but i am not goood in arry to achive it with array!! my xml has one set of data only .
 
i won't comment on the fact that arrays are part of just about every language and you simply can't get far without understanding them.

nor about how easy the php manual is to follow...

Code:
<?php
$xml = <<<EOL
<playing>
<artist>Cindy</artist> 
<song>echo</song> 
<image>[URL unfurl="true"]http://www.somesite.com/song_images/cindy.jpg</image>[/URL]
<rating>3.5</rating>
<songid>4736</songid>
</playing>
EOL;
//create an instance of an xml parser

$parser = xml_parser_create();

//set some parser options

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 1);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);

//parse the xml
xml_parse_into_struct($parser, $xml, $vals, $keys);

//free the parser
xml_parser_free($parser);

//set up an array of tags we want to capture
$wantedtags = array("IMAGE", "ARTIST", "SONG", "RATING", "SONGID");

//iterate through the parsed xml
foreach ($vals as $tag){
	//test to check whether the tag is one we want
	if (in_array($tag['tag'], $wantedtags)){
		//create a variable variable and assign the value to it
		${$tag['tag']} = $tag['value'];
	}
}

//this is just included to show you the output
echo '<table border="1">';
foreach ($wantedtags as $wtag){
	echo '<tr><td>$'.$wtag.'</td><td>'.${$wtag}.'</td></tr>';
}
echo '</table>';
?>
 
jpadie thanks for your nice code. But my xml file is in a remote site .How to parss that?your example showes xml in same file.Furthermore, how to refrence song,image,artist individully since i need it in my program later. The xml file has one set of data at all time and it is rss feed changing for each song.
 
to get the xml just retrieve it like any file on a remote server. if you had read the manual this would be obvious to you.

Code:
$url = '[URL unfurl="true"]http://www.example.com/path/to/some.xml';[/URL]
$xml = file_get_contents($url);

if you had run the code i posted above, you would see how to reference the individual elements of the xml. please show some initiative.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top