PodMaster has asked for the wisdom of the Perl Monks concerning the following question:

I expect "teh_xml_to_hashref" and "teh_xml_to_hashref2" to both return a hash equivalent of the xml in $ARGH. Both XML::Simple and XML::TokeParser should accept a scalar ref as raw xml. XML::Simple also accepts a string as raw xml, while XML::TokeParser is not supposed to. Looking at my test script below, I am more than confused ~ can someone please set me straight (i'd be oh so glad if it is something stupid i am doing)
use XML::Simple; use XML::TokeParser; use Data::Dumper 'DumperX'; my $ARGH = q{<NODE reputation="0" createtime="2001-08-24 08:01:08" id= +"107642">podmaster</NODE>}; print "\n\n0000000000000000000000000000000000000000000000000000000\n\n +"; print 'warn DumperX teh_xml_to_hashref($ARGH);'."\n"; eval'warn DumperX teh_xml_to_hashref($ARGH);'; warn "oh $! and $@ " if $@; print 'warn DumperX teh_xml_to_hashref2($ARGH);'."\n"; eval'warn DumperX teh_xml_to_hashref2($ARGH);'; warn "oh $! and $@ " if $@; print "\n\n111111111111111111111111111111111111111111111111111111111\n +\n"; print 'warn DumperX teh_xml_to_hashref(\$ARGH);'."\n"; eval'warn DumperX teh_xml_to_hashref(\$ARGH);'; warn "oh boy $! and $@ " if $@; print 'warn DumperX teh_xml_to_hashref2(\$ARGH);'."\n"; eval'warn DumperX teh_xml_to_hashref2(\$ARGH);'; warn "oh boy $! and $@ " if $@; print "\n\n22222222222222222222222222222222222222222222222222222222222 +22\n\n"; print 'warn DumperX teh_xml_to_hashref(${\$ARGH});'."\n"; eval'warn DumperX teh_xml_to_hashref(${\$ARGH});'; warn "damn $! and $@ " if $@; print 'warn DumperX teh_xml_to_hashref2(${\$ARGH});'."\n"; eval'warn DumperX teh_xml_to_hashref2(${\$ARGH});'; warn "damn $! and $@ " if $@; print "\n\n33333333333333333333333333333333333333333333333333333333333 +3\n\n"; my %DBHASH = ( 1000 => $ARGH ); warn DumperX \%DBHASH; print 'warn DumperX teh_xml_to_hashref(\$DBHASH{1000});'."\n"; eval'warn DumperX teh_xml_to_hashref(\$DBHASH{1000});'; warn "damn $! and $@ " if $@; print 'warn DumperX teh_xml_to_hashref2(\$DBHASH{1000});'."\n"; eval'warn DumperX teh_xml_to_hashref2(\$DBHASH{1000});'; warn "damn $! and $@ " if $@; print "\n\n44444444444444444444444444444444444444444444444444444444444 +4444444\n\n"; my $refa = \$DBHASH{'1000'}; warn $refa. ' refa '; warn $$refa. ' $refa '; print 'warn DumperX teh_xml_to_hashref($refa);'."\n"; eval'warn DumperX teh_xml_to_hashref($refa);'; warn "damn $! and $@ " if $@; print 'warn DumperX teh_xml_to_hashref2($refa);'."\n"; eval'warn DumperX teh_xml_to_hashref2($refa);'; warn "damn $! and $@ " if $@; sub teh_xml_to_hashref { my $fref = shift; return XMLin($fref); } sub teh_xml_to_hashref2 { my $htmlref = shift; my $t; my $p = XML::TokeParser->new($htmlref, ErrorContext => 3 ) or die +$!; my %N=(); $t = $p->get_tag('NODE'); $N{'reputation'} = $t->[1]->{'reputation'}; $N{'createtime'} = $t->[1]->{'createtime'}; $N{'id'} = $t->[1]->{'id'}; $t = $p->get_trimmed_text('/NODE') or die $!; $N{'content'} = $t; return \%N; } __END__ F:\dev\XmlPimp\crazydev\XmlPimp>perl xml.t 0000000000000000000000000000000000000000000000000000000 warn DumperX teh_xml_to_hashref($ARGH); $VAR1 = { 'reputation' => '0', 'content' => 'podmaster', 'createtime' => '2001-08-24 08:01:08', 'id' => '107642' }; warn DumperX teh_xml_to_hashref2($ARGH); oh Invalid argument and Invalid argument at xml.t line 62. at xml.t line 12. 111111111111111111111111111111111111111111111111111111111 warn DumperX teh_xml_to_hashref(\$ARGH); oh boy and not well-formed at line 1, column 6, byte 6 at c:/Perl/site/lib/XML/Pa +rser.pm li ne 168 at xml.t line 17. warn DumperX teh_xml_to_hashref2(\$ARGH); $VAR1 = { 'reputation' => '0', 'content' => 'podmaster', 'createtime' => '2001-08-24 08:01:08', 'id' => '107642' }; 2222222222222222222222222222222222222222222222222222222222222 warn DumperX teh_xml_to_hashref(${\$ARGH}); $VAR1 = { 'reputation' => '0', 'content' => 'podmaster', 'createtime' => '2001-08-24 08:01:08', 'id' => '107642' }; warn DumperX teh_xml_to_hashref2(${\$ARGH}); damn Invalid argument and Invalid argument at xml.t line 62. at xml.t line 29. 333333333333333333333333333333333333333333333333333333333333 $VAR1 = { '1000' => '<NODE reputation="0" createtime="2001-08-24 08:01 +:08" id="1 07642">podmaster</NODE>' }; warn DumperX teh_xml_to_hashref(\$DBHASH{1000}); damn No such file or directory and not well-formed at line 1, column 6, byte 6 at c:/Perl/site/lib/XML/Pa +rser.pm li ne 168 at xml.t line 36. warn DumperX teh_xml_to_hashref2(\$DBHASH{1000}); $VAR1 = { 'reputation' => '0', 'content' => 'podmaster', 'createtime' => '2001-08-24 08:01:08', 'id' => '107642' }; 444444444444444444444444444444444444444444444444444444444444444444 SCALAR(0x1abf060) refa at xml.t line 44. <NODE reputation="0" createtime="2001-08-24 08:01:08" id="107642">podm +aster</NOD E> $refa at xml.t line 45. warn DumperX teh_xml_to_hashref($refa); damn No such file or directory and not well-formed at line 1, column 6, byte 6 at c:/Perl/site/lib/XML/Pa +rser.pm li ne 168 at xml.t line 48. warn DumperX teh_xml_to_hashref2($refa); $VAR1 = { 'reputation' => '0', 'content' => 'podmaster', 'createtime' => '2001-08-24 08:01:08', 'id' => '107642' }; F:\dev\XmlPimp\crazydev\XmlPimp>

 

Look ma', I'm on CPAN.


** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
(podmaster: rrrr) Re: XML Simple and Tokeparser troubles
by PodMaster (Abbot) on Apr 11, 2002 at 13:42 UTC
    Well, after consulting with someone very close with XML::Parser, I took his advice, and switched to XML::Twig ... however I'm only halfway there (generating hash works, generating xml not so good ... i'm confident i'll get help on that one ;)
    use Data::Dumper 'DumperX'; use XML::Twig; my $ARGH = q{ <NODE reputation="0" createtime="2001-08-24 08:01:08" id="107642">podm +aster</NODE> }; print DumperX teh_xml_to_hashref($ARGH); print DumperX teh_xml_to_hashref('<o dear="xmltwig">oh how I love thee +</o>'); print DumperX teh_xml_to_hashref('<crack >babies>'); die teh_hashref_to_xml(teh_xml_to_hashref($ARGH)); sub teh_xml_to_hashref { my $xml = shift; my $t = new XML::Twig; $t->safe_parse($xml); return { crack => babies => 6 => 6 } if $@; $t = $t->root; # there is only NODE return { content => $t->text, # cause it's not an att map { $_ => $t->att($_) } $t->att_names, }; } # where is the love? the above in under 10 minutes, but this? sub teh_hashref_to_xml { my $xref = shift; my $t = new XML::Twig; $t->parse('<NODE></NODE>'); $t = $t->root; # there is only NODE $t->set_text($xref->{content}); delete $xref->{content}; for my $att(keys %$xref) { # $t->set_att( $key => $$xref{$key} ); # d'oh, where'em crack babies at?:) $t->set_att( $att => $$xref{$att} ); } return $t->print; } __END__ $VAR1 = { 'reputation' => '0', 'createtime' => '2001-08-24 08:01:08', 'content' => 'podmaster', 'id' => '107642' }; $VAR1 = { 'content' => 'oh how I love thee', 'dear' => 'xmltwig' }; $VAR1 = { 'crack' => 'babies', '6' => 6 }; <NODE createtime="2001-08-24 08:01:08" id="107642" reputation="0">podm +aster</NODE>Died at twig.t line 11.

     

    Look ma', I'm on CPAN.


    ** The Third rule of perl club is a statement of fact: pod is sexy.
Re: XML Simple and Tokeparser troubles
by stephen (Priest) on Apr 08, 2002 at 21:42 UTC

    I'm no XML::Simple expert, but I don't read the POD docs as saying that XMLin() will take a scalar ref as an argument. It'll accept a string, a filename, or an IO::Handle.

    stephen