Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: newbie regex question: substituting repeating occurences for different replacements

by AcidHawk (Vicar)
on Jul 08, 2003 at 14:01 UTC ( [id://272280]=note: print w/replies, xml ) Need Help??


in reply to newbie regex question: substituting repeating occurences for different replacements

This got me thinking... I often create xml but always with the first tag being different for each .. well where the tag <row> exists here I normally only need

<row>a</row> <row2>b</row2>
So here is my stab at doing this with XML::Simple. Please comment on how I can refine this... but I do know it outputs what was asked for... ;-D

#! /usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; my (%h); my $xs = XML::Simple->new( keeproot => 1, noattr => 1, noescape => 1); open (FILE, ">>./Test.xml") or die "Cannot create Test.xml: $!\n"; while (<DATA>) { delete $h{row}; my $count = 0; chomp; my @line = split(/;/); foreach my $var (@line) { $count++; my $tag = "col" . $count; $h{'row'}{$tag} = $var; } print Dumper(\%h); print FILE $xs->XMLout(\%h); } close(FILE); __DATA__ a;b;c;d;e f;g;h i; j

-----
Of all the things I've lost in my life, its my mind I miss the most.
  • Comment on Re: newbie regex question: substituting repeating occurences for different replacements
  • Select or Download Code

Replies are listed 'Best First'.
(jeffa) 2Re: substituting repeating occurences ... (XML::Generator::DBI / DBD::AnyData version)
by jeffa (Bishop) on Jul 09, 2003 at 22:05 UTC
    Personally, i think you shouldn't concern yourself with incrementing XML tags (even though i played along and posted a solution myself). The first <foo> tag encountered is the first, and the second <foo> tag encountered is the second, and so on - as long as that's how you intend to read the data. XML::Simple handles this by exposing an option (forcearray). Since order will be preserved, why bother with incrementing the tags? (And besides, wouldn't that id number be better as an attribute instead?)

    I posted a few solutions to converting CSV to XML over at CSV to XML (the quick and dirty way). Ultimately, CSV::XML looks the easiest, but i still dig using XML::Generator::DBI. One option i didn't try at the time, however, was DBD::AnyData. Here's one that follows the <colN> naming convention and uses the previous two modules ... but, caveats:

    1. commas are used instead of semi-colons - DBD::AnyData does not handle them, but could be written to do so, if desired. However, note that CSV stands for comma. ;)
    2. ughh ... i have to specify the maximum number of columns to be expected. This is not something i advocate, but then again, this is all because the <colN> have to be incremented.
    Enough, here's the code:
    use strict; use warnings; use DBI; use XML::Generator::DBI; use XML::Handler::YAWriter; my $max = 5; my $data = join(',',map"col$_",1..$max) . do {local $/;<DATA>}; my $dbh = DBI->connect('dbi:AnyData(RaiseError=>1):'); $dbh->func('test', 'CSV', [$data], 'ad_import'); my $generator = XML::Generator::DBI->new( Handler => XML::Handler::YAWriter->new(AsFile => '-'), dbh => $dbh, Indent => 1, ); $generator->execute('select * from test'); __DATA__ colContents,nextColContents,lastColContents colContents2,nextColContents2,lastColContents2 colContents3,nextColContents3,lastColContents3 a,b,c,d,e f,g,h,i j,k l m,n,o

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://272280]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-18 10:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found