codeinphp Posted May 3, 2015 Share Posted May 3, 2015 (edited) Having some issues with appending an XML file thru a foreach loop. First some info on the script. The script opens an XML file on my server and reads thru the contents extracting url values. It takes that url value (stored in $urlhtml) and passes to a curl function which in turn curls the url, parse information and passes that thru variable $html back to script. The value of $html is then appended to the XML in a particular location. The initial curl ($urlhtml) is a website with TV shows and the $urlhtml represents a particular show. The $html value represents each episode returned from the curl, which needs to be stored under the show being curled. Following an example of the xml sturcture and below that is the script that does this. What is happening is that the url of the is taken from the XML, the foreach loop is entered, the curl is made and the url for each episode is extracted and appended into the xml, but the problem is that instead of the episode url being appended under respective item element in the xml(where the show is located), all the urls for all episodes are appended under the first item in the XML file. So after executing, if I look at the xml file, all of my shows are under a different item element as needed, but all episode titles and episode urls for all shows are located under one item element, not under it's respective show. Any help or advice in getting episodes under it's show would be greatly appreciated. Thanks <feed> <item> <showname>Name of Show Goes Here</showname> <streamUrl>http://locationofshowstream.com</streamUrl> <episodetitle>Name of Episode Goes Here</episodetitle> <episodeurl>URL of the Episode Goes Here</episodeurl> </item> </feed> $xml = new SimpleXMLElement(file_get_contents($xmllocation)); if (!$xml){ echo 'ERROR WHILE PARSING DOCUMENT'; } else{ foreach($xml->item as $item){ $url=$item->streamUrl; if (!$url){ echo 'ERROR WHILE PARSING'; }else{ $html = file_get_html($url); $xmlappend=simplexml_load_file($xmllocation); $elements=$html->find('a[class=main-tt]'); foreach($elements as $element) { $stitle=$element->title; $streamer=$element->href; $urlhtml=curlurl2($streamer); //CURL EACH $episodeurl=substr($urlhtml,2); //CREATES THE FULL URL PATH $epitem= $xmlappend->item; $episodetitle=$xmlappend->item->addChild('eptitle', $stitle); $episode=$xmlappend->item->addchild('epiurl', $episodeurl); } $xmlappend->asXML($xmlsave); } } } Edited May 3, 2015 by codeinphp Quote Link to comment Share on other sites More sharing options...
requinix Posted May 4, 2015 Share Posted May 4, 2015 With just ->item, SimpleXML will always get the first element named "item". Use $xmlappend->addChild to create a new item each time, put that in a variable, and add the title and URL to that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.