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

Hi Folks I have an text file which is pre-edited. I need to convert into an xml file(which accords with a DTD). Txt file eg:
[[a]] some text here[[/a]] [[b]] some more text [[/b]] [[a]] some text again[[c]]moer here..[[/c]] [[/a]]
This is only a simpler version of the text file. There are many pre-editing tags([[a]],[[b]],[[c]]..) which represent different segment of xml document. Any element can come inside anyother elements.
Can you suggest some way of converting it to an xml document. Or is there any modueles available to simplify nested element conversion? Your suggestion would be of immense help to me! Thanks in advance

Edit: removed 'l33t-speak', fixed [s davorg

Replies are listed 'Best First'.
Re: using perl to convert 2 xml file
by Joost (Canon) on Aug 25, 2004 at 10:41 UTC
Re: using perl to convert to xml file
by mirod (Canon) on Aug 25, 2004 at 13:50 UTC

    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]]
Re: using perl to convert to xml file
by Anonymous Monk on Aug 26, 2004 at 04:10 UTC
    Thanx Guys
    I'll consider using both methods!
    Thanx once again!
    Lily123