How to convert csv file to XML using XML::Simple?

How to convert csv file to XML using XML::Simple?

am 02.11.2007 03:54:09 von James Egan

I need to take a comma separated value (csv) file and convert it to XML.
I'm trying to do this with XML::Writer. Using XML::Writer I can see how
to convert FROM an XML document, but I don't see how to take a csv file
and convert it to XML. If I had a csv file like this:

Robert, Smith, 123 Main St.
Jane, Smith, 456 Market St.
William, Watson, 789 First Ave.


How would I convert that to an XML file like this:




Robert
Smith

123 Main St.



Jane
Smith
456 Market St.



William
Watson
789 First Ave.





Any help would be greatly appreciated!

That should read all refer to XML::Simple

am 02.11.2007 04:02:35 von James Egan

That should read all refer to XML::Simple, not XML::Writer.

Re: How to convert csv file to XML using XML::Simple?

am 02.11.2007 11:55:11 von Paul Lalli

On Nov 1, 10:54 pm, James Egan wrote:
> I need to take a comma separated value (csv) file and convert it to XML.
> I'm trying to do this with XML::Writer. Using XML::Writer I can see how
> to convert FROM an XML document, but I don't see how to take a csv file
> and convert it to XML. If I had a csv file like this:
>
> Robert, Smith, 123 Main St.
> Jane, Smith, 456 Market St.
> William, Watson, 789 First Ave.
>
> How would I convert that to an XML file like this:
>
>
>
>
> Robert
> Smith
>

123 Main St.

>
>
> Jane
> Smith
>
456 Market St.

>

>
> William
> Watson
>
789 First Ave.

>

>
>
> Any help would be greatly appreciated!

What have you tried so far? How did it not work the way you wanted?

I would use Text::CSV to parse the CSV, and create a hash out of each
line, then pass the resulting data structure to XMLout(), like so:

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
use XML::Simple;

my @col_names = qw/FIRST_NAME LAST_NAME ADDRESS/;
my $csv = Text::CSV->new();
my $xml = { CONTACT => [ ] };
while () {
chomp;
$csv->parse($_);
my @cols = $csv->fields();
my %hash = map { $col_names[$_] => $cols[$_] } 0..$#cols;
push @{$xml->{CONTACT}}, \%hash;
}
print XMLout($xml, RootName => "ADDRESS_BOOK", NoAttr => 1);
__DATA__
Robert, Smith, 123 Main St.
Jane, Smith, 456 Market St.
William, Watson, 789 First Ave


Paul Lalli

Re: How to convert csv file to XML using XML::Simple?

am 02.11.2007 12:42:10 von Tad McClellan

James Egan wrote:

> Robert, Smith, 123 Main St.
> Jane, Smith, 456 Market St.
> William, Watson, 789 First Ave.
>
>
> How would I convert that to an XML file like this:
>
>
>
>
> Robert
> Smith
>

123 Main St.

>
>
> Jane
> Smith
>
456 Market St.

>

>
> William
> Watson
>
789 First Ave.

>

>


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

while ( ) {
chomp;
my($first, $last, $adr) = split /,\s*/;
print <
$first
$last
$adr


ENDCONTACT
};


__DATA__
Robert, Smith, 123 Main St.
Jane, Smith, 456 Market St.
William, Watson, 789 First Ave.
--------------------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: How to convert csv file to XML using XML::Simple?

am 03.11.2007 14:57:25 von James Egan

On Fri, 02 Nov 2007 11:42:10 +0000, Tad McClellan wrote:

> while ( ) {
> chomp;
> my($first, $last, $adr) = split /,\s*/;
> print < >
> $first
> $last
>

$adr

>
> ENDCONTACT
> };
>
>
> __DATA__
> Robert, Smith, 123 Main St.
> Jane, Smith, 456 Market St.
> William, Watson, 789 First Ave.
> --------------------------


Thanks. I found a really great module named XML::Generator. Most modules
like XML::Writer and XML::Simple use a .xml file for input, and then
output XML.

Re: How to convert csv file to XML using XML::Simple?

am 03.11.2007 14:58:07 von James Egan

On Fri, 02 Nov 2007 03:55:11 -0700, Paul Lalli wrote:
> print XMLout($xml, RootName => "ADDRESS_BOOK", NoAttr => 1);
> __DATA__
> Robert, Smith, 123 Main St.
> Jane, Smith, 456 Market St.
> William, Watson, 789 First Ave
>
>
> Paul Lalli



Thanks. I found a really great module named XML::Generator. Most modules
like XML::Writer and XML::Simple use a .xml file for input, and then
output XML.

Re: How to convert csv file to XML using XML::Simple?

am 03.11.2007 15:58:20 von Paul Lalli

On Nov 3, 9:58 am, James Egan wrote:
> On Fri, 02 Nov 2007 03:55:11 -0700, Paul Lalli wrote:
> > print XMLout($xml, RootName => "ADDRESS_BOOK", NoAttr => 1);
> > __DATA__
> > Robert, Smith, 123 Main St.
> > Jane, Smith, 456 Market St.
> > William, Watson, 789 First Ave
>
> > Paul Lalli
>
> Thanks. I found a really great module named XML::Generator.
> Most modules like XML::Writer and XML::Simple use a .xml file for
> input, and then output XML.

You're making no sense. XML::Simple, as I demonstrated above, does
not take *any* file for input. It takes a reference to a hash
structure, and then outputs XML.

Why are you bothering to thank me when you obviously didn't read my
post?

Paul Lalli

Re: How to convert csv file to XML using XML::Simple?

am 12.11.2007 04:11:49 von sln

On Sat, 03 Nov 2007 07:58:20 -0700, Paul Lalli wrote:

>On Nov 3, 9:58 am, James Egan wrote:
>> On Fri, 02 Nov 2007 03:55:11 -0700, Paul Lalli wrote:
>> > print XMLout($xml, RootName => "ADDRESS_BOOK", NoAttr => 1);
>> > __DATA__
>> > Robert, Smith, 123 Main St.
>> > Jane, Smith, 456 Market St.
>> > William, Watson, 789 First Ave
>>
>> > Paul Lalli
>>
>> Thanks. I found a really great module named XML::Generator.
>> Most modules like XML::Writer and XML::Simple use a .xml file for
>> input, and then output XML.
>
>You're making no sense. XML::Simple, as I demonstrated above, does
>not take *any* file for input. It takes a reference to a hash
>structure, and then outputs XML.
>
>Why are you bothering to thank me when you obviously didn't read my
>post?
>
>Paul Lalli
Why would anybody want to thank you? You know absolutely nothing
about XML

Re: How to convert csv file to XML using XML::Simple?

am 12.11.2007 04:13:43 von sln

On Fri, 02 Nov 2007 11:42:10 GMT, Tad McClellan wrote:

>James Egan wrote:
>
>> Robert, Smith, 123 Main St.
>> Jane, Smith, 456 Market St.
>> William, Watson, 789 First Ave.
>>
>>
>> How would I convert that to an XML file like this:
>>
>>
>>
>>
>> Robert
>> Smith
>>

123 Main St.

>>
>>
>> Jane
>> Smith
>>
456 Market St.

>>

>>
>> William
>> Watson
>>
789 First Ave.

>>

>>
>
>
>--------------------------
>#!/usr/bin/perl
>use warnings;
>use strict;
>
>while ( ) {
> chomp;
> my($first, $last, $adr) = split /,\s*/;
> print < >
> $first
> $last
>
$adr

>

>ENDCONTACT
>};
>
>
>__DATA__
>Robert, Smith, 123 Main St.
>Jane, Smith, 456 Market St.
>William, Watson, 789 First Ave.
>--------------------------
You can't write xml this way can you????
No, I didn't think so.