Jump to content

PHP Parse XML Save To TXT


ripcurlksm

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.