Jump to content

Recommended Posts

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>
Link to comment
https://forums.phpfreaks.com/topic/35850-php-parse-xml-save-to-txt/
Share on other sites

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.