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

Please give me some idea of how to convert the data in this format to XML format
[DATA] data1 [ENTRY] entrying here [STORY] story details
where the output looks like this
<?xml version="1.0"?> <DATA>data1</DATA> <ENTRY>entrying here</ENTRY> <STORY>Story details </STORY>

Replies are listed 'Best First'.
Re: convert to XML
by ww (Archbishop) on Aug 12, 2009 at 06:19 UTC
    SHORT ANSWER: Search for an appropriate module on CPAN (or PPM if using Windows and ActiveState).

    STANDARD ANSWER: What have you tried? Show us the code and tell us how it's failing.

    FOOD-FOR-THOUGHT ANSWER: Your question can be answered in a simple minded way, assuming what you litterally asked is really what you want, with appropriate Regular Expressions or in half a dozen other ways. But what do you want to happen if your source looks like this?

    [DATA] data1 [ENTRY] entry here [STORY] story details [DATA] [ENTRY] another entry here [STORY] another story

    You'll get better help here if your question is precise and well-bounded...and when you show your efforts.

      #!/usr/bin/perl print qq|<?xml version="1.0"?>|; print "\n"; while($_ = <DATA>){ if (/^\[\w+\]/ .. /^\[\w+\]/) { $save = $_; $save =~ s/\[/</g; $save =~ s/\]/>/g; print $save; } }
      __DATA__ [DATA] data1 [ENTRY] entry here [STORY] story details [DATA] [ENTRY] another entry here [STORY] another story
      I have tried sometime like this. But I am not able to store the values in a variable
        I am not able to store the values in a variable

        See perlre, in particular how to use $1 and its cousins, to extract matching parts from a regexp.

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: convert to XML
by ack (Deacon) on Aug 12, 2009 at 17:20 UTC

    Here is one approach. It is not particularly elegent, but it gets the job done.

    I presume that you're getting the data through some input stream (file reads or something similar). Note that in the above code I have not modeled that. Instead I set up a simple string variable $in_str to duplicate the specific input that you specified. That part of the challenge is up to you to work out. But the heart of the code below should take care of the conversion for you.

    Note also that I have put in several print statements so that you can see what's going on along the way and to confirm that the code, in execution, is progressing properly. You, obviously, wouldn't need them in your code (though they represent a strategy to help be sure that an algorithm is behaving the way one expects/desires).

    The other responders offer very good and sage advice and it would behoove you to head their suggestions and advice. Also, as throughout Perl, TMTOWDI; my approach is neither elegent nor, by any means, the only way to get the job done.

    ack Albuquerque, NM
Re: convert to XML
by Anonymous Monk on Aug 12, 2009 at 07:04 UTC
Re: convert to XML
by bichonfrise74 (Vicar) on Aug 12, 2009 at 19:21 UTC
    Try this?
    #!/usr/bin/perl use strict; print qq{<?xml version="1.0"?>\n}; my $tag; while(<DATA>) { chomp; if ( /^\[(\w+)\]/ ... /^\w+/ ) { $tag = $1 if $1; print "<$tag>$_</$tag>\n" if ( ! /^\[/ ); } } __DATA__ [DATA] data1 [ENTRY] entrying here [STORY] story details
      #!/usr/bin/perl use strict; print qq{<?xml version="1.0"?>\n}; my $tag; while(<DATA>) { chomp; if ( /^\[(\w+)\]/ ... /^\w+/ ) { $tag = $1 if $1; print "<$tag>$_</$tag>\n" if ( ! /^\[/ ); } } __DATA__ [DATA] data1 [ENTRY] entry here [STORY] story details [DATA] [ENTRY] another entry here [STORY] another story [KEY] [WRITER] Han Staff
      For the above code, the output is below
      <?xml version="1.0"?> <DATA>data1</DATA> <ENTRY>entry here</ENTRY> <STORY>story details</STORY> <DATA>another entry here</DATA> <STORY>another story</STORY> <KEY>Han</KEY>
      KEY WRITER Han Staff For KEY there is no string, for WRITER there is 2 lines. The value of WRITER is for KEY in the output. Please can you tell me how can I solve this
        The value of [WRITER] is for [KEY] in the output.

        The perlop says that the right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. Meaning that once /^\[(\w+)\]/ is true, it's not evaluated anymore, so $1 is still KEY when print is performed.

Re: convert to XML
by grantm (Parson) on Aug 14, 2009 at 00:21 UTC

    The example output you're trying to generate isn't actually XML. Every XML document must have a single top-level element that contains all the other elements. E.g:

    <?xml version="1.0"?> <STUFF> <DATA>data1</DATA> <ENTRY>entrying here</ENTRY> <STORY>Story details </STORY> </STUFF>