Parsing XML properly
I am working with Songkick API and need to parse the XML data from a URL to grab a nested url and redirect to it. I can't seem to get the parse the XML properly. Here is what I have now.
1. <?php
2. $songkick_artist = $_GET['id'];
3.
4. $xmlDoc = new DOMDocument();
5. $xmlDoc->load("http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_artist&apikey=songkickapikey");
6.
7. $x开发者_开发技巧 = $xmlDoc->documentElement;
8. foreach ($x->childNodes AS $item)
9. {
10. print $item->[]
11. print $item->nodeName . " = " . $item->nodeValue . "<br />";
12. }
13. ?>
I am getting this error:
Parse error: syntax error, unexpected '[', expecting T_STRING or T_VARIABLE or '{' or '$' on line 10
There are so many ways to parse XML info that I am quite confused right now. Any help on this would be greatly appreciated.
Updated 4-13-11
I changed my code to this now:
<?php
$doc = new DomDocument;
// We must validate the document before referring to the id
$doc->validateOnParse = true;
$doc->Load("http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_artist&apikey=songkickapikey");
echo "The element whose id is myelement is: " .
$doc->getElementById('myelement')->tagName . "\n";
?>
and I am getting a 403 forbidden error when $doc->Load("URL"); is fired...
What gives?
Your problem is a simple parse error. You lack an ; on line 10. That might solve it?
What do you want to do in line 10? Your syntag is wrong:
print $item->[]
$item is an Object so you can use $item->doSomeMethod() to do doSomeMethod but you can't write $item->[].
Got this figured out.
$request_url = urlencode ("http://api.songkick.com/api/3.0/search/artists.xml?query=$songkick_artist&apikey=songkickapikey"); $xml = simplexml_load_file($request_url) or die("feed not loading"); $artist = $xml->results ->artist["uri"];
The urlencode is the main key on this since the url will not parse correctly...
精彩评论