Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how to convert tab separated text lines into xml files

by osprofi (Initiate)
on Feb 19, 2012 at 17:43 UTC ( #954892=perlquestion: print w/ replies, xml ) Need Help??
osprofi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks I could not find in the archives or Cpan smth to solve the following task: I have a text file with the following format which I want to convert to a correct xml file:
ABC [TAB]DEF [TAB][TAB]GHI etc.
my question: is there a perl module for this task available or do you know a workable solution; Thanks for your advise Greetings, Peter

Comment on how to convert tab separated text lines into xml files
Download Code
Re: how to convert tab separated text lines into xml files
by moritz (Cardinal) on Feb 19, 2012 at 18:04 UTC

    There are modules on CPAN for reading such files (for example Text::CSV), and there are lots of modules for producing XML.

    You'll just have to put them together in a small script to do what you want.

Re: how to convert tab separated text lines into xml files
by Corion (Pope) on Feb 19, 2012 at 18:11 UTC

    You don't tell us what XML schema you intend to output, nor do you show any code you have attempted to write yourself.

    My approach would be:

    #!perl -wle while (<>) { s!^!<row><value>!; s!\t!</value><value>!g; s!$!</value></row>!; print; };

    If you need more structure in your XML, you will have to tell us what structure.

      You would also need to escape & and < in the text, and make sure you get the encoding right.
Re: how to convert tab separated text lines into xml files
by NetWallah (Abbot) on Feb 20, 2012 at 05:40 UTC
    To generate XML, your source data also needs to be structured.

    Your data currently appears to be close to YAML format, so if you did convert it to YAML, you could do something like this:

    use strict; use warnings; use YAML; my $stuff = <<YAML; --- ABC: DEF: - GHI YAML my $perl_structure = Load $stuff; printit ($perl_structure); sub printit{ my $this = shift; if (ref $this eq "" ){ print $this; return; } if (ref $this eq "ARRAY"){ print "<value>"; printit ($_) for @$this; print "</value>\n"; return; } #print "Got " . ref ($this) . "\n"; for my $k (keys %$this){ print "<$k>\n"; printit($_) for $this->{$k}; print "</$k>\n"; } }
    Output:
    <ABC> <DEF> <value>GHI</value> </DEF> </ABC>
    However - if your input is even slightly more complicated than this, I would recommend using a "real" XML generator module. (XML::Simple if you dont have experience with these).

                “PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.”
            ― Jon Ribbens

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://954892]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2014-10-23 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (128 votes), past polls