You could also generate a SAX provider for your format, which would allow you to use for example XML::SAX::Writer to output it. What you get with this method is the automatic escaping of text, and the ease of adding other XML constructs, attributes, comments... when you need it. See Perl SAX 2.0 Binding for more info.

Here is a code example:

#!/usr/bin/perl -w use strict; use XML::SAX::Writer; my $DEBUG=0; # set to 1 to debug my $w = XML::SAX::Writer->new; # see the man for XML::SAX::Writer to o +utput to file $w->start_document(); # in your example you don't have a root element, so you need one my $root= 'root'; $w->start_element( { Name => $root, }); while( <DATA>) { # the split will return (char, tag, char, tag, char...) # the list always starts and ends with a char my @tokens= split( m{\[\[ # [[ ([^\]]*) # tag \]\] # ]] }x ); print join( ' - ', map { "'$_'" } @tokens), "\n" if( $DEBUG); while( @tokens) { my $chars= shift @tokens; $w->characters( { Data => $chars} ); my $tag= shift @tokens or last; if( $tag=~ m{^/(.*)$}) { $w->end_element( { Name => $1 }); } else { $w->start_element( { Name => $tag }); } } } $w->end_element( { Name => $root, }); $w->end_document; __DATA__ [[a]] some text here[[/a]] [[b]] some more text [[/b]] [[a]] some text again[[c]]more here..[[/c]] [[/a]] [[a]] some text again[[c]]more here..[[/c]][[/a]]

In reply to Re: using perl to convert to xml file by mirod
in thread using perl to convert to xml file by lily123

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.