develooper Front page | perl.beginners | Postings from May 2007

Re: How to split a large string with repeating delimiters into multiple substrings

Thread Previous | Thread Next
From:
Dave Gray
Date:
May 25, 2007 10:38
Subject:
Re: How to split a large string with repeating delimiters into multiple substrings
Message ID:
ed4f42020705251037p6474d629pd8e2da86aae6181c@mail.gmail.com
Hi Michael,

On 5/23/07, Michael Goopta <michaelgoopta@yahoo.com> wrote:
> How can I split the below string and get the multiple
> web-addresses in a list: (i.e. the strings between <upsl-url>
> and </upsl-url>
>
> <upsl-url>http://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124&t=ts/r:199x199</upsl-url><upsl-url>http://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124&t=ts/r:199x199</upsl-url>
> </ad-type>

Any time you can avoid using regular expressions to parse xml-ish
data, it's usually easier to do so.

#!/usr/bin/perl
use strict;
use warnings;

use XML::Simple;
use Data::Dumper;

my $xml;
{ local $/ = undef;
  $xml = <DATA>; }
# fixup unescaped ampersands
$xml =~ s/&/&amp;/g;

my $parsed = XMLin($xml, ForceArray => 1);

print Dumper($parsed);

__DATA__
<ad-type>
<upsl-url>http://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124&t=ts/r:199x199</upsl-url><upsl-url>http://view-preprod.admission.net/abc/mactive/_NJMG_0002029003-01/i-1.JPG?t=tr/m:FitPad/w:199/h:124&t=ts/r:199x199</upsl-url>
</ad-type>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About