Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: httpwebwitch

Message Too Old, No Replies

XML help

Easy question.... I think

   
11:29 am on Oct 27, 2006 (gmt 0)

5+ Year Member



I am trying to display the Artist and Title from this XML feed (http://example.com/NRG_FM.xml) on my php based homepage. I have tried numerous ways of doing it but none of them seem to work. i am a real noob to the world of XML. Can anyone help or at least guide me in the right direction?

Thanks you

[edited by: jatar_k at 12:30 am (utc) on Oct. 31, 2006]
[edit reason] no urls thanks [/edit]

12:54 pm on Oct 27, 2006 (gmt 0)

5+ Year Member



Hi,

This should do it:


<?php
$file = file_get_contents("http://example.com/NRG_FM.xml");
$doc = new DOMDocument();
$doc->loadXML($file);
foreach($doc->getElementsByTagName('Entry')->item(0)->attributes as $attribute){
if($attribute->name == "Title" $attribute->name == "Artist"){
echo $attribute->name . ": " . $attribute->value . "<br>";
}
}
?>

(ps. you need to change any broken vertical bars to solid vertical bars)

Andrew

[edited by: Little_G at 12:59 pm (utc) on Oct. 27, 2006]

[edited by: jatar_k at 12:30 am (utc) on Oct. 31, 2006]

12:04 pm on Oct 28, 2006 (gmt 0)

5+ Year Member



hi thank you very much for you help but I get the following error when I try to run this script:

Parse error: parse error, unexpected T_OBJECT_OPERATOR in f:\webserver\test\playlist.php on line 5

12:50 pm on Oct 28, 2006 (gmt 0)

5+ Year Member



Hi,

Are you using PHP5?

Andrew

8:48 am on Nov 2, 2006 (gmt 0)

10+ Year Member



Hi,
is it necessary to use PHP5 for this?
And how would be the solution for PHP4?

Regards,
Baron

1:20 pm on Nov 2, 2006 (gmt 0)

5+ Year Member



I upgraded to PHP5 and now everytime I try to run this script it gives me the following error:

Parse error: parse error, unexpected T_STRING in f:\webserver\playlist.php on line 6

also once this script runs properly how would I go about inserting the info into other php pages?

7:55 pm on Nov 2, 2006 (gmt 0)

5+ Year Member



Hi,

Firstly, grooverince, did you change any broken vertical bars to solid vertical bars?

Secondly, baron13, you need to assign the return value of a method to a variable and then call the methods from that, so:

$doc->getElementsByTagName('Entry')->item(0)->attributes

becomes
$a = $doc->getElementsByTagName('Entry')
$a->item(0)->attributes;

I think the above code is right I don't have PHP4 installed so I can't test it.

Andrew

1:05 pm on Nov 4, 2006 (gmt 0)

5+ Year Member



Thank you very much for all your help Little G the script works like a charm. Now for the final question, how would I go about displaying the information from this script on a seperate php page?
2:03 pm on Nov 4, 2006 (gmt 0)

5+ Year Member



Hi,

The easiest way would probably be to use include [php.net].

Andrew