in reply to Re^2: Another problem with XML parser
in thread Another problem with XML parser

toolic had a really good piece of advice that might have been glossed. XML::Parser is not newbie friendly. XML::Twig or XML::LibXML are likely what you want to work with.

I'm not sure I followed your example code in your question. Now that you've given some sample data, could you give a description of what desired output/outcome is? You might well get an example solution in Twig and libxml.

Replies are listed 'Best First'.
Re^4: Another problem with XML parser
by Paulux (Acolyte) on Nov 23, 2009 at 11:26 UTC
    thanx for the privided example, but at the moment I'm tryng to follow the gmargo's one, just because there's another complex logic with the parser that I'm using. But, if i have another problem of cutted datas i'll try to use your. B/R
Re^4: Another problem with XML parser
by Anonymous Monk on Nov 16, 2009 at 10:23 UTC
    Here is the other part of the code that follows the xml file example.

      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>

      If you were using strict (or my code :) you'd see the difference between $InstanceNumber and $IstanceNumber.

        But in your code in the handlers start and end you don't empty out the variable $Number, is just because you check if you are in the node with the variable $inHeader = 1; ? B/R
      I'm sorry, but the anonymous monker it's me. I didn't log in