Solved

Tags again

Posted on 2002-05-29
1
178 Views
Last Modified: 2010-03-05
I need a script that will close specific tags in an xml script.


I had a script which is below that kind of works but it skips every second tag. I'm not sure why.


#! /usr/local/bin/perl -w

while (<>) {
   if((/<([^>]*)>/) && (!$inTag)) {
       $inTag = 1;
       $startTag = $1;
   }
   elsif(/<([^>]*)>/) {
       print "</$startTag>\n";
       $inTag = 0;
   }
   print;
}





The xml file looks like this:

<hockey>

The Toronto Maple leafs scored 3-0 in their final game to win the series.

How will Toronto do in the next round?

Any comments are welcome.

<baseball>
</baseball>





Should look like this:

<hockey>

The Toronto Maple leafs scored 3-0 in their final game to win the series.

How will Toronto do in the next round?

Any comments are welcome.
</hockey>

<baseball>
</baseball>  
0
Comment
Question by:2xdafun
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 7043617
while( <> ){
    if( /<\/\Q$startTag\E>/i ){
        $startTag = '';
    }elsif( /<([^\/>][^>]*)>/ ){
        print "<\/$startTag>\n\n" if $startTag;
        $startTag = $1;
    }
    print;
}
print "<\/$startTag>\n\n" if $startTag;

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now