#!/usr/bin/perl use strict; use warnings; my $tag; my $output; my $fh; my $deletestrings; while (<DATA>) { s/\s*<\s*(\/?)\s*(\w+)\s*>\s*/$1?"\n":"\n\{$2\}\n"/ge; chomp; s/[\cA-\cZ]//g; # To remove control characters s/^[\\|<]$//g; # To delete the character like \ and < at the e +nd of the line s/[\\|<]$//; # To delete the character like \ and < at the beg +ining of the line s/^\s+//g; # To remove multiple spaces at the begining of the + line s/\s+$//g; # To remove spaces at the end of the line if(/^{(.*)}$/) { # match {METATAG} line $fh = output($output, $tag, $fh); $output = ""; $tag = $1; } else { # not a {TAG} line next unless($tag); next if(/^\s*$/); s/\\//g; $output .= ($output) ? " $_" : "<$tag>$_"; } } $fh = output($output, $tag, $fh); if($fh) { print $fh "</ROOT>\n"; close($fh); } exit(0); # Subroutine to open the file with the filename as {SOURCETAG} # Subroutine to open the file with the filename as {SOURCETAG} sub output { my ($output, $tag, $fh) = @_; if($output) { if($output =~ m/<SOURCE>(.*)/) { if($fh) { print $fh "</ROOT>\n"; close($fh); } open($fh, '>', "$1.xml") or die "$1.xml: $!"; print $fh "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RO +OT>\n"; } print $fh "$output</$tag>\n"; } return($fh); } __DATA__ {SOURCE} 0904230634 {DATE} 090424 {EDITION} 1 {HEADLINE} heredero del famoso deportista mexicano, lucha por enaltecer la vida y + obra del autor de sus dM-mas {SOURCE1} Por Gisela Orozco 312.527.8461/ Chicago\ <byline> Por Gisela Orozco< ttl>312.527.8461/ Chicago</ttl>
When executed, it creates a file called 0904230634.xml in the current directory. The output is
<?xml version="1.0" encoding="UTF-8"?> <ROOT> <SOURCE>0904230634</SOURCE> <DATE>090424</DATE> <EDITION>1</EDITION> <HEADLINE>heredero del famoso deportista mexicano, lucha por enaltecer + la vida y obra del autor de sus dM-mas</HEADLINE> <SOURCE1>Por Gisela Orozco 312.527.8461/ Chicago {byline}Por Gisela Or +ozco{ttl}312.527.8461/ Chicago</SOURCE1> </ROOT>
So Instead of
<?xml version="1.0" encoding="UTF-8"?> <ROOT> <SOURCE>0904230634</SOURCE> <DATE>090424</DATE> <EDITION>1</EDITION> <HEADLINE>heredero del famoso deportista mexicano, lucha por enaltecer + la vida y obra del autor de sus dM-mas</HEADLINE> <SOURCE1>Por Gisela Orozco 312.527.8461/ Chicago</SOURCE1> <byline>Por Gisela Orozco</byline> <ttl>312.527.8461/ Chicago</ttl> </ROOT>
So please tell me why the string is replaced only with {} and further it is not converted to < as for other tags

In reply to Re^2: replace string by Anonymous Monk
in thread replace string by Anonymous Monk

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.