ripcurlksm Posted January 26, 2007 Share Posted January 26, 2007 I am familiar with parsing XML feeds and printing them using PHP. However I need to find a way to print/save the XML contents to a .TXT file. Something like this:somefile.txt-----------------------<tr><td><a href='#'>Link 1</a></td></tr><tr><td><a href='#'>Link 2</a></td></tr><tr><td><a href='#'>Link 3</a></td></tr><tr><td><a href='#'>Link 4</a></td></tr><tr><td><a href='#'>Link 5</a></td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/35850-php-parse-xml-save-to-txt/ Share on other sites More sharing options...
ripcurlksm Posted January 26, 2007 Author Share Posted January 26, 2007 I found this perl script, but how do I implement it?[code]#!/usr/local/bin/perl# Convert rss to text - rss2txt.pl## Usage : rss2txt.pl <RSS file> > textfile.txt## Author : Kyo Nagashima# E-Mail : [email protected]# URL : http://hail2u.net/## This program is free software; you can redistribute it and/or modify# it under the same terms as Perl itself.use strict;die "Usage: rss2txt.pl <RSS file> > textfile.txt\n" unless @ARGV == 1;use Jcode;use XML::RSS;use LWP::Simple;my $rss = new XML::RSS;my $j = new Jcode;my $content;my $text;my $arg = shift;if ($arg =~ /http:/i) { $content = get($arg); die "Error: Cannot find $arg\n" unless $content; eval { $rss->parse($content); }; die "Error: Cannot parse $arg\n$@\n" if $@;}else { $content = $arg; die "Error: Cannot find $arg\n" unless -e $content; eval { $rss->parsefile($content); }; die "Error: Cannot parse $arg\n$@\n" if $@;}my $chname = &trim($rss->{'channel'}->{'title'});my $chlink = &trim($rss->{'channel'}->{'link'});my $chdesc = &trim($rss->{'channel'}->{'description'});$text .= qq|$chname\n|;$text .= qq|$chlink\n|;$text .= qq|$chdesc\n|;$text .= qq|\n|;for my $item (@{$rss->{'items'}}) { my $itemname = &trim($item->{'title'}); my $itemlink = &trim($item->{'link'}); my $itemdesc = &trim($item->{'description'}); $text .= qq|$itemname\n|; $text .= qq|$itemlink\n|; $text .= qq|$itemdesc\n|;}print STDOUT $j->set(\$text)->sjis;exit;# ---------------------------------------------------------------------------- #sub trim{ my $value = $_[0]; if ($value) { $value =~ s/^\s+//; $value =~ s/\s+$//; } return $value;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35850-php-parse-xml-save-to-txt/#findComment-169972 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.