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.

In reply to XML Simple and Tokeparser troubles by PodMaster

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.