Hey Monks ... Prayers might be too late for me since I've had the inevitable, after coding my web application with XML::Simple I came to a full halt. I needed to use CDATA fields to encapsulate user-changable HTML data in its XML configuration files. Not only CDATA was needed but namespace support; for languages and itunes podcasting.

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 ...

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;
Now, still, my questions remain:
  1. How could I best "auto-detect" for html code which I do not want escaped or automatically define a declaration to CDATA without breaking the "simple arrayref" ? I thougth about a ${example}->{'tagname:cdata'} solution would be "best".
  2. would it be possible for fields that have html but need multi-language support? ex: ${main}->{'outline:cdata:en'} = "HTML CODE" ? would I need to make my own checking routine then which will puts the ${main} to its seperate array extracting the tag(s)/optional cdata/optional language field ?
  3. How can I put the internal create_element only to be used by XMLTwout ? since it also needs to call itself..
  4. will I ever get a life or eternal happyness? ;)

In reply to XML::Simple functionality with XML::Twig ? by freakingwildchild

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.