Solved

Tags again

Posted on 2002-05-29
1
185 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
crawling gofundme 4 126
Add additional column to .csv using Perl. 8 141
Insert Text into odbc.ini file 15 66
Writing a parser for java language 4 79
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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