Hello Monks and thanks for bearing with me on this, my first project.

I have successfully written a program that parses a large XML document using twig. The XML file is 500 products with variable attributes. One might be a table with certain dimensions. Another might be a personal organizer with a weekly calendar and pockets for a calculator/cellphone etc. In order to get an idea of what variety of tables I need to use in my database, I wrote the following to summarize the xml.

#!/bin/perl use strict; use warnings; use XML::Twig; use Tie::IxHash; my %Items; my $Output_Filehandle; tie %Items, "Tie::IxHash"; my $twig=XML::Twig->new( twig_handlers => {_all_ => sub {my $Item_master_Ancestory = $_->ancestors; my $element_match = ($_->tag); my $text = ($_->trimmed_text); my $coupled = join( ' - ' => " "x$Item_master_Ancestory, +$element_match,keys %{$_->atts},values %{$_->atts},$text); if (!defined $Items{$coupled}){$Items{$coupled}=1} else {$Items{$coupled}++;} }, } ); $twig->parsefile( '500syncItemMaster.xml'); # build it $twig->purge; # clear end of document from memory open(SUMMARY, ">United perl parser summary.txt"); my @k = keys %Items; foreach my $k (@k) {print SUMMARY ("$k => $Items{$k}\n");};
I used join to combine all the relevant data into a unique hash key and then set the value to the number of times that key occurs in the xml file. This gave me a nice breakdown of what unique items are in the xml. My problem is twofold.

First, some of the elements are picking up the text from their children while others don't. This is very strange to me.

Second, the Tie module didn't keep the entire xml file ordered properly. for example

<Catalog> <item> <quantities>z <prices> <sellingpoints> <item> <quantities> <prices> <sellingpoints>
becomes:

In reply to Maintaining parent child order when printing summarized XML Twig by Mr.Churka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.