Hi,

I know the basics of perl and really need a dummies guide (simple steps) to get the following working. I came across the code below and downloaded the Twig.pm and Xpath.pm files into C:\Perl\lib (Windows machine) from CPAN and I cannot get the code to compile. Can someone give me guidance with ABC (simple) steps to follow as to what I need to download (links appreciated) and what I need to copy where to get this to work. Alternatively if someone has got a perl to CSV function that is written simple Perl that would be appreciated. Thank you very much.

use XML::Parser;
use XML::Twig;
use AnyData;

my $input_xml = "test.xml";
my $output_csv = "test.csv";

$flags->{record_tag} = 'ITEM';
adConvert( 'XML', $output_file_xml, 'CSV', $output_csv, $flags );

Recommended Answers

All 5 Replies

The error I get is "Can't locate XML/Twig.pm in @INC (@INC contains: c:/perl/site/lib c:/perl/lib .) "

OK, some progress but still seems a long way off from success :)

I tried the following

perl -e shell -MCPAN
install XML::Parser

A lot of output and it seems to be doing something. However, it finally stops at the following error messages.

dmake: Error code 129, while making '..\blib\arch\auto\XML\Parser\Expat\Expat.d
ll'
dmake: Error code 255, while making 'subdirs'
dmake -- NOT OK
Running make test
Can't test without successful make
Running make install
make had returned bad status, install seems impossible

cpan>

OK, I've made progress but still no-go. I've found this error in the output. Expat is installed on the Windows machine in C:\Program Files (x86)\Expat 2.1.0 . I've really got stuck here as I don't know where perl is expecting the Expat files nor do I know how to add the options to Makefile.PL. Please help with some basic instruction. I really need to get this to work...

Error ...

Expat must be installed prior to building XML::Parser and I can't find
it in the standard library directories. Install 'expat-devel' package with your
OS package manager. See 'README'.

Or you can download expat from:

http://sourceforge.net/projects/expat/

If expat is installed, but in a non-standard directory, then use the
following options to Makefile.PL:

EXPATLIBPATH=...  To set the directory in which to find libexpat

EXPATINCPATH=...  To set the directory in which to find expat.h

For example:

perl Makefile.PL EXPATLIBPATH=/home/me/lib EXPATINCPATH=/home/me/include

Different Direction, but still struggling. I found the following code which works. OK, can anyone take me from this point to easily converting $d2 into CSV format. I've been working at this almost non-stop and will be very grateful for any help. Please note, final XML files will have multi-levels.

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

# create object
$xml = new XML::Simple;
# read XML file
#$data = $xml->XMLin("data.xml");
$data = $xml->XMLin(join('', <DATA>));
# print output
$d2=Dumper($data);
print "$d2";

DATA
<records>
<record>
<id>1</id>
<name>George Foo</name>
<phone>555.666.7777</phone>
</record>
<record>
<id>2</id>
<name>Betty Bar</name>
<phone>666.777.8888</phone>
</record>
</records>

Hi xyciana,
This answer might be coming a bit late, but am sure it will be useful.

Using the data you posted, you can simply use XML::Simple as you are doing with Text::CSV or you use a better xml parser like XML::LibXML.

Yes, XML::Simple can simply take on your dataset here but it might be a different story with a larger and more complicated xml document.

You could do this to get what you wanted I suppose, since you didn't show how your output is to look.

use warnings;
use strict;
use XML::Simple qw(:strict);
use Text::CSV;

my $xml = XML::Simple->new( ForceArray => 1, KeyAttr => {} );
my $rec = $xml->XMLin(
    do { local $/; <DATA> }
);

my $csv = Text::CSV->new( { binary => 1 } )
  or die "can't open file:", Text::CSV->error_diag();

foreach my $nodes ( @{ $rec->{record} } ) {
    $csv->print( \*STDOUT, [ map { @{ $nodes->{$_} } } sort keys %{$nodes} ] );
    print $/;
}


__DATA__
<records>
<record>
<id>1</id>
<name>George Foo</name>
<phone>555.666.7777</phone>
</record>
<record>
<id>2</id>
<name>Betty Bar</name>
<phone>666.777.8888</phone>
</record>
</records>

The output is like so:

1,"George Foo",555.666.7777
2,"Betty Bar",666.777.8888

You might get more XML modules in CPAN. Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.