Solved

Tags again

Posted on 2002-05-29
1
166 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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now