Page 1 of 1

PHP Parse XML Save To TXT

Posted: Fri Jan 26, 2007 12:55 pm
by ripcurlksm
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>

Posted: Fri Jan 26, 2007 1:23 pm
by Kieran Huggins

Code: Select all

header('Content-type: text/plain')
will send it to the browser in plain-text view

Loading it into a DOM and then using:
http://www.php.net/manual/en/function.d ... t-save.php
will save it as a local file.

Posted: Fri Jan 26, 2007 1:30 pm
by ripcurlksm
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Cool.

Also I found this perl script, but how do I implement it? Is this the same thing?

[syntax="perl"]
#!/usr/local/bin/perl

# Convert rss to text - rss2txt.pl
#
# Usage  : rss2txt.pl <RSS file> > textfile.txt
#
# Author : Kyo Nagashima
# E-Mail : kyo@hail2u.net
# 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;
}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jan 26, 2007 1:33 pm
by Kieran Huggins
It would seem the usage is:
your file wrote:Usage: rss2txt.pl <RSS file> > textfile.txt
have you tried it? Or any of the suggested methods?