Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[plugin] jk_parse_xml: get data from XML feeds
- jk_parse_xml v0.41 download
So I have a client that wants to incorporate content from her Last.fm feed into her site, so I thought I’d whip up a plugin that grabs an XML document and outputs it as a list of items based on a specified form. Still working out the kinks (caching is definitely needed!), but its a start. As I am a novice at working with XML data, any tips appreciated.
As this plugin works by targeting specific parent elements and their related children, it will not work with XML documents nested more then two elements deep. Also note that to load an external feed, this plugin requires either allow_url_fopen turned on, or CURL enabled. Finally, be aware that this plugin only works with PHP5…
I have tried this with plain RSS feeds, as well as XML documents generated by Last.fm. Also tried it with Flickr, which also seems to work, but make sure you specify that the feed is in the rss2 format and not atom format (ie, use: http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&format=rss2 rather than http://api.flickr.com/services/feeds/photos_public.gne?tags=cat, which is the default).
If you find some use in this plugin, consider making a donation to our publishing project Fillip — donations will encourage development of this plugin beyond my own needs.
This plugin requires PHP 5’s DOM functions to work.
Change Log
- 0.2: Fixed major bug in parser
- 0.3: Added support for local xml files
- 0.4: Added support for cached remote files
- 0.4: Fixed remote user agent bug
- 0.41: Fixed bug with remote fopen
Last edited by Jeff_K (2008-03-10 22:07:36)
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
So what is different between this plugin and bit_rss ?
ps… Filip looks very cool
Last edited by colak (2007-03-16 07:34:20)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
From what I can tell, bit_rss only parses RSS feeds (?). This plugin tries to grab data from other types of XML documents. Just as you can split an RSS document into items , you can also split a Last.fm XML document into tracks , or an address book XML document into contacts. But yeah, bit’s plugin has many many more bells and whistles…
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
This is EXACTLY what I what looking for. I hope you can get caching working, I probably won’t switch to this until you do. It’s annoying that the RSS feed on Last.fm has both the artist and the song title output with <txp:title> when using bit_rss, preventing styling each item independently. Also the RSS feed doesn’t have the album name included. It’s actually a pretty crappy feed overall. The XML feed looks a lot better. Good luck again, sorry I’m a noob and I know nothing about programming anything.
I did find this method using SimplePie that seems to be getting album names and individual styling of track elements using just the rss feed, not sure how they are pulling it off.
Last edited by aswihart (2007-04-14 15:20:29)
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
I will be posting an update later today that offers a potential fix for the caching issue, and a work around for last.fm intigration. More in a bit…
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
I uploaded a new version of the plugin (v.0.3) which includes support for local files, which is much faster then pulling xml files from a remote server. Simply point to the file on your server and set the “local_file” attribute to 1.
Combined with a working cron job, this should (at least in many cases) alieviate the need for caching. For Last.fm, I use the code below run once an hour to synch a local file with the Last.fm feed. I have been testing it for the past week on a client’s site (http://amillionkeys.com) and it seems to work quite well. Just set the $xml and $xmlfile variables accordingly and call up the resulting file through the plugin.
$xml = "http://ws.audioscrobbler.com/1.0/user/username/recenttracks.xml";
// if remote fopen off, try curl
if (ini_get('allow_url_fopen') == 0) {
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $xml);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$xml = curl_exec($ch);
curl_close($ch);
}
$lines = count(explode("<br />", nl2br($xml)));
if ($lines > 3) {
$xmlfile = fopen($currentDir."recenttracks.xml", "w");
fwrite($xmlfile, $xml);
fclose($xmlfile);
}
Last edited by Jeff_K (2007-04-16 04:46:20)
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
wow, awesome plugin. just gave it a quick testdrive with my youtube favorites and it worked flawless — at least when grabbing the local file. but i might set up a cron job anyways and then fetch the local file for enhanced performance…
Offline
#8 2007-05-31 05:14:11
- Vitruvius
- Plugin Author
- Registered: 2004-09-21
- Posts: 125
Re: [plugin] jk_parse_xml: get data from XML feeds
This plugin sounds perfect for my needs, but I am getting an error with TXP 4.0.4 r2334 just by enabling the plugin:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/usr/public_html/textpattern/lib/txplib_misc.php(528) : eval()’d code on line 45
The above errors were caused by the plugin:jk_parse_xml
As I said – I get this error on any page just by enabling the plugin – not even using its tags anywhere.
Any help would be appreciated.
SH
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
Are you running PHP 4? PHP 5 is required for the plugin to work. Many applogies for the confusion. I will emphasize that in the documentation above.
Last edited by Jeff_K (2007-05-31 08:22:50)
Offline
#10 2007-05-31 09:53:26
- Vitruvius
- Plugin Author
- Registered: 2004-09-21
- Posts: 125
Re: [plugin] jk_parse_xml: get data from XML feeds
That would be it :) I did suspect as much after doing a bit of digging. I am using PHP 4.4 something-or-other. I’ll see if it can be upgraded without too much fuss…
SH
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
this would be very useful for an old project that is still unfinished… unfortunately i can’t get it to work with xml files that are on a remote server. here is the problem / tag error:
tag_error <txp:jk_parse_xml xml="http://www.hhv.de/partner/singleLink.php?products_id=110791&cid=1169475819&XML=1" local_file="0" form="hhvItem" parents="record" elements="artist,recordTitle,coverTnImageFile" /> -> Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Start tag expected, '<' not found in Entity, line: 1 on line 40
if i’m calling the same xml file locally, it works.
allow_url_fopen is on, and CURL is enabled. DOM functions are enabled as well. server is running php5.
any ideas?
Offline
Re: [plugin] jk_parse_xml: get data from XML feeds
i found the problem and fixed it. there is no user agent set when grabbing the file with CURL which caused a warning on the remote server. that warning hindered the parser from parsing the xml file properly. specifying some random user agent fixed it…
Offline