Hi all, yes, I am in need of some Perl Wisdom :)
I have a script which parses an XML file to Excell. All goes fine, but I'm quite stuck on the referencing.
My current xml looks like this:
<root> <SUB CLASSNAME="SUB1"> <Element1 Type="Type1">Type 1 value</Element1> <Element2 Type="Type2">Type 2 value</Element2> </SUB> </root>
But I don't know ( I know, it's simple but I don't get it) how to parse an xml looking like this...
<SUB CLASSNAME="SUB1"> <INNERSUB CLASSNAME="INNERSUB1"> <Element1 Type="Type1">Content</element1> <Element2 Type="Type2">Content</element2> </INNERSUB> </SUB>
How do I reach Type and Content now ?? (My working script is linked at the bottom with full xml) This is a piece of my code:
my $xs1 = XML::Simple->new(); my $doc = $xs1->XMLin($file, keyattr=>['CLASSNAME'], ForceContent=>1, +ForceArray=>1); use Data::Dumper; local $Data::Dumper::Indent=1; for my $sub ( sort keys %{ $doc->{SUB} } ){ $rowCount = $tellerR; $worksheet->Cells($rowCount, $colCount)->{Value} = $sub; for my $elem ( sort keys %{ $doc->{SUB}{$sub} } ){ for my $element ( @{ $doc->{SUB}{$sub}{$elem} } ){ $worksheet->Cells($rowCount, $colCount)->{Value} = $$element{Type}; $worksheet->Cells($rowCount, $colCount)->{Value} = $$element{content}; } }
The complete working script is at: Script file
and an example xml as well : The XML
And even as a ZIP/RAR at : Rar Package
Feel free to dowload and reuse. But please help me out

In reply to Newbie XML Simple reference by Sporti69

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.