freakingwildchild has asked for the wisdom of the Perl Monks concerning the following question:
Since I've had incorporated XML::Simple in a lot of my code it would be a major pain if I had to change all my code again to support XML::DOM, XML::Twig or XPATH; just because of those two missing features in XML::Simple.
I've chosen TWIG because it looked the most natural to XML::Simple in a way (cope with me here) ; and ; I had nice example code to start from from the developer; although; I am a little bit stuck now; since I want a CDATA field to be "automagically CDATA" and a normal textfield to be normally TEXT without using DTD's.
I was thinking about two solutions; one would be naming my tag outline.cdata or something like that and to autodetect for the presence of such tag; or; the second solution would be to create/find a "html detection routine" which will automatically see if the code does not fit the XML standards to be inbetween normal tags without CDATA and add such field as CDATA; although; both ways I find pretty much spaghetti-coding and probably also a hog.
updated node: What I want is literally quite "XML::simple"; I want XML::Twig to use a normal Arrayref just like XML::simple does; instead of manipulating the entire structure; which is easy with the "simplify" parameter to read, but not so easy to write (to my knowledge?). Add to that, the cherry on top, namespace support (any ideas?)
The reasons: less memory, faster, easier and it's usable with my code which is based on XML::Simple. I am also sure other people might be wanting this when they want CDATA and namespace support; which XML::Twig supports; but not in that "XML::Simple" way ...
Now, still, my questions remain:use XML::Twig; use Data::Dumper; # let's emulate XMLin even more simple (for now) sub XMLTwin { if (-e $_[0]) { return XML::Twig->new->parsefile($_[0])->simplify( k +eyattr => []); } } # let's emulate XMLout even more simple than XML::Simple (for now) sub XMLTwout { my $elt; my $twig = XML::Twig->new(pretty_print => 'indented', empty_tags = +> 'html'); my $xmltag = "opt"; sub create_element { # needs to be a internal subroutine only for XM +LTwout... my $gi = shift; my $data = shift; my $t = XML::Twig::Elt->new($gi); if (ref $data) { while (my ($k,$v) = each(%$data)) { if ($k ne "outline") { create_element($k, $v)->paste(last_ch +ild => $t); } else { $t->insert_new_elt( last_child => $k => { '#CDATA' +=> 1 }, $v); } } } else { $t->set_text($data); } $t; } if ($_[1]) { $xmltag = $_[1] }; $elt = create_element($xmltag => $_[0]); $twig->set_root($elt); undef($elt); # let's clear some memory here print Dumper($elt); return $twig->sprint(); } # some test code ; my $file = 'test.xml'; my $xmldata = XMLTwin($file); # let's get it in my $xmlout = XMLTwout($xmldata); # let's get it out ... print $xmlout;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Simple functionality with XML::Twig ?
by saberworks (Curate) on Jun 15, 2006 at 15:42 UTC | |
by freakingwildchild (Scribe) on Jun 15, 2006 at 15:51 UTC | |
by saberworks (Curate) on Jun 15, 2006 at 16:34 UTC | |
|
for future reference - old code
by freakingwildchild (Scribe) on Jun 15, 2006 at 17:35 UTC |