in reply to Re: XML::Simple processing
in thread XML::Simple processing

Thanks for the kick in the butt. I have spent most of my evening on this, and have gotten good results with XML::DOM. It is able to parse my document and pull together the information I need in a recursive loop ...
sub printNode { my ($theNode) = @_; my $thisType = $theNode->getNodeType; my $nodeList = $theNode->getChildNodes; my $name = $theNode->getNodeName; my $attLength; my $length = $nodeList->getLength; my $attList = $theNode->getAttributes; if( $attList ){ $attLength = $attList->getLength }; #print $sep x ($depth), "-NodeName: '$name' (type $thisType, $leng +th children)\n"; $depth++; for(my $i=0; $i<$length; $i++) { my $node = $nodeList->item($i); my $theType =$node->getNodeType; my $j=$i+1; if ($theType == ELEMENT_NODE ) { #print $sep x ($depth), "[$j]Element Node: '", $node->getT +agName, "'\n"; #Recursive call to itself printNode( $node ); } elsif ($theType == TEXT_NODE ) { my $temp = $node->getData; #Sub out the tabs and newlines with text equivalents $temp =~ s/\n/\\n/g; $temp =~ s/\t/\\t/g; unless ($name eq "provinces") { print $sep x ($depth), $name.qq~="~.$temp.qq~"\n~; } } else { print $sep x ($depth), "[$j]UNKNOWN Node: ", $node->getNod +eName, "\n"; } } $depth--; }
I get the following output
path="smallbusiness" en_title="Small Business" fr_title="Petites Entreprises" hideFromMenu="false" hideBreadcrumbs="false" path="products" en_title="Products and Services" fr_title="Produits et Services" hideFromMenu="false" hideBreadcrumbs="false" path="wireless" en_title="Wireless" fr_title="Sans fil" hideFromMenu="false" hideBreadcrumbs="false" path="devices" en_title="Phones and Devices" fr_title="Tlphones et Appareils" portalPageLabel="smb_products_services_wir +eless_devices" hideFromMenu="false" hideBreadcrumbs="false" path="details" en_title="Details" fr_title="Dtails" portalPageLabel="smb_products_services +_wireless_devices_details" hideFromMenu="true" hideBreadcrumbs="true" path="plans" en_title="Plans" fr_title="Forfaits" portalPageLabel="smb_products_services_wir +eless_plans" hideFromMenu="false" hideBreadcrumbs="false"
The last piece is being able to have the output in XML. the start of each node (i.e. "path" should be prepended with <menuitem and the end of each node (i.e. hideBreadcrumbs should end with ">" *or* "/>". Depending if it has children the XML tag should be left open, if no children, the XML tag should be closed. I can't seem to insert these tags within the recursive logic without having many repeating tags. Thanks a lot for any assistance you might be able to provide.

Replies are listed 'Best First'.
Re^3: XML::Simple processing
by GrandFather (Saint) on Dec 22, 2010 at 20:18 UTC

    Reading between the lines (which you've still not shown) you seem to be translating tags to turn a database represented in XML into a nested menu structure represented in XML. In the absence of the sample code I suggested earlier I've invented a sample and you can either modify it or write your own if my invention doesn't match your need:

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new( start_tag_handlers => {_all_ => \&convert,}, pretty_print => 'indented_a' ); $twig->parse(*DATA); $twig->print(); sub convert { my ($twig, $elt) = @_; $elt->set_tag('menuitem'); } __DATA__ <lvl_1 path="smallbusiness" en_title="Small Business" fr_title="Petites Entreprises" hideFromMenu="false" hideBreadcrumbs="false" > <lvl_2 path="products" en_title="Products and Services" fr_title="Produits et Service +s" hideFromMenu="false" hideBreadcrumbs="false" /> </lvl_1>

    Prints:

    <menuitem en_title="Small Business" fr_title="Petites Entreprises" hideBreadcrumbs="false" hideFromMenu="false" path="smallbusiness"> <menuitem en_title="Products and Services" fr_title="Produits et Services" hideBreadcrumbs="false" hideFromMenu="false" path="products" /> </menuitem>
    True laziness is hard work
Re^3: XML::Simple processing
by mjscott2702 (Pilgrim) on Dec 22, 2010 at 10:49 UTC
    Thanks for the kick in the butt.

    GrandFather was actually trying to help you - by posting a concise, easily digestable description of the problem, you are much more likely to get the pointers you are looking for.

    I have spent most of my evening on this

    Well, it is your evening - don't expect people to wade through screenfuls of text to try to figure out your problem. See above.

      mjscott2702:

      I notice from your user node that you're in Scotland, so a quick note: he used "Thanks for the kick in the butt" as slang for "Oops! Thanks for pointing me in the right direction", with a bit of self deprecation (like Homer Simpson's "D'oh!") thrown in. "Thanks for the whack with the cluebat" would be an equivalent expression (among a bazillion others).

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Maybe I was a little hasty to jump on that, the tone more than anything else seemed rather ungracious to me, but maybe that's just me - apologies all round.

        Yep, I am in Scotland, but lived in the United States for several years, so can (usually) pick up on slang from either side of the pond - sometime humor (or humour), even self-deprecating, can come across differently from that intended on the page. My bad!

      I was by no means trying to insult anyone; just trying to articulate I was working toward a solution. thanks.