in reply to XML::Simple processing

There is far too much data there for me to be bothered with. How about you generate a small sample script that includes embedded xml data and generates sample output that demonstrates the issue you are having? That has a few advantages:

  1. we don't have to wade through a mass of text to try and understand your problem
  2. you can focus attention on the real issue you are having
  3. you have a simple test bed for trying different techniques
  4. you have a simple way to see is different solutions work
  5. we don't have to whittle your sample data down to something manageable
  6. through seeing your code we can assess your Perl fu and tailor answers and code to suit
True laziness is hard work

Replies are listed 'Best First'.
Re^2: XML::Simple processing
by LPC2010 (Initiate) on Dec 22, 2010 at 04:53 UTC
    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.

      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
      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.

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