This is mildly idiomatic (the grep/map, for example, and there is probably an equally terse but less idiomatic version). I hope it's otherwise serviceable and interesting. XML::LibXML and Text::CSV_XS for more fun and deeper options.

Aside: nodeName ne '#text' is more readable but nodeType != 3 is a little more portable (older versions call text nodes "text").

use strict; use warnings; use XML::LibXML; use Text::CSV_XS; my $doc = XML::LibXML->new->parse_fh(\*DATA); my $root = $doc->getDocumentElement; my $csv = Text::CSV_XS->new({ eol => "\n" }); my ( $ip_node ) = $root->findnodes("Header/IpNumber"); my $ip = $ip_node->textContent; open my $out, ">", "$ip.csv" or die "Coulnd't open $ip.csv for writing: $!"; $csv->print( $out, [ $ip, undef ] ); for my $content_element ( $root->findnodes("ContentElement") ) { my @elements = map { $_->textContent } grep { $_->nodeName ne "#text" } $content_element->childNodes; $csv->print( $out, \@elements ); } __DATA__ <someRoot> <Header> <IpNumber>AC_123</IpNumber> </Header> <ContentElement> <IdNumber>xyxyxyxy-yy</IdNumber> <InstanceNumber>001463010000016</InstanceNumber> </ContentElement> <ContentElement> <IdNumber>ceiling-cat</IdNumber> <InstanceNumber>77777777777</InstanceNumber> </ContentElement> <ContentElement> <IdNumber>basement-cat</IdNumber> <InstanceNumber>666666666666666666</InstanceNumber> </ContentElement> </someRoot>

In reply to Re^5: Another problem with XML parser by Your Mother
in thread Another problem with XML parser by Paulux

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.