hmadhi has asked for the wisdom of the Perl Monks concerning the following question:

I need some guidance dealing with complex XML structures like the one
below. My code below does not give the desired results.
use DBI; my $dbh = DBI->connect('dbi:AnyData(RaiseError=>1):'); $dbh->func( 'catalog', #alias 'XML', #format of input 'catalog.xml', #source {col_names=>'product,catalog_item,item_number,price,size,color_swatch +'}, 'ad_catalog' # ); my $sth = $dbh->prepare("SELECT * FROM catalog"); $sth->execute(); while (my $row = $sth->fetch) { print "@$row \n"; }

produces the following result:
cardigan.jpg Cardigan Sweater
QWZ567139.95RedBurgundyRedBurgundyRRX985642.50RedNavyBurgundyRedNavyBurgundyBlackNavyBlackBurgundyBlack
QWZ567139.95RedBurgundyRedBurgundy
RRX985642.50RedNavyBurgundyRedNavyBurgundyBlackNavyBlackBurgundyBlack
catalog.xml
<?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_swatch> </size> <size description="Large"> <color_swatch image="navy_cardigan.jpg">Navy</ color_swatch> <color_swatch image="black_cardigan.jpg">Black</ color_swatch> </size> <size description="Extra Large"> <color_swatch image="burgundy_cardigan.jpg">Burgundy</ color_swatch> <color_swatch image="black_cardigan.jpg">Black</ color_swatch> </size> </catalog_item> </product> </catalog>

Replies are listed 'Best First'.
Re: DBD:AnyData on Complex XML
by eff_i_g (Curate) on Nov 13, 2008 at 15:05 UTC
    What is the desired result—to have the missing attributes included?
    What is your ultimate goal here? XPath may be a better tool for the job. And there's always XML::Twig :)
      What is my desired result ?
      Good Question
      Something like :
      product catalog_item item_number price size color
      ------------------------------------------------------------------------------
      By the way how do I use XPath ?
        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.