in reply to Re^2: DBD:AnyData on Complex XML
in thread DBD:AnyData on Complex XML

I was trying to get at the data structure that you're after and how you want to use the data (convert it to another format, filter it, prune some of the XML, etc.).

If you're going to query on certain conditions, you can use XPath with modules like XML::XPath and XML::Twig. If you simply want a Perl data structure, something like XML::Simple will do.

Also see the Perl-XML FAQ.

Replies are listed 'Best First'.
Re^4: DBD:AnyData on Complex XML
by hmadhi (Acolyte) on Nov 17, 2008 at 09:51 UTC
    I want to import into a mysql database, so I would like to convert to csv.
      Here's an example using Text::CSV and XML::Twig to get you started:
      use strict; use warnings; use Text::CSV; use XML::Twig; my $CSV = Text::CSV->new(); my $XML = XML::Twig->new( twig_handlers => { product => sub { my @data; push @data, $_->att('description'); my $c_i = $_->first_child('catalog_item'); push @data, $c_i->att('gender'); ### Gather the other fields here, then... $CSV->print(*STDOUT, \@data); print "\n"; }, }, pretty_print => 'indented', ); $XML->parsestring(*DATA); __DATA__ <?xml version="1.0"?> <?xml-stylesheet href="catalog.xsl" type="text/xsl"?> <!--<!DOCTYPE catalog SYSTEM "catalog.dtd">--> <catalog> <product description="Cardigan Sweater" product_image="cardigan.jpg" +> <catalog_item gender="Men's"> <item_number>QWZ5671</item_number> <price>39.95</price> <size description="Medium"> <color_swatch image="red_cardigan.jpg">Red</color_swatch> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color +_swatch> </size> <size description="Large"> <color_swatch image="red_cardigan.jpg">Red</color_swatch> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color +_swatch> </size> </catalog_item> <catalog_item gender="Women's"> <item_number>RRX9856</item_number> <price>42.50</price> <size description="Small"> <color_swatch image="red_cardigan.jpg">Red</color_swatch> <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color +_swatch> </size> <size description="Medium"> <color_swatch image="red_cardigan.jpg">Red</color_swatch> <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color +_swatch> <color_swatch image="black_cardigan.jpg">Black</color_swatc +h> </size> <size description="Large"> <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> <color_swatch image="black_cardigan.jpg">Black</color_swatc +h> </size> <size description="Extra Large"> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color +_swatch> <color_swatch image="black_cardigan.jpg">Black</color_swatc +h> </size> </catalog_item> </product> </catalog>