Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Replacing XML Tag name with another

by sandy88 (Initiate)
on Sep 27, 2011 at 11:29 UTC ( #928068=perlquestion: print w/ replies, xml ) Need Help??
sandy88 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Scripters, I am newbie to this perl scripting, I want to replace <H1> tag to <Heading 1> and <H2> tag to <Heading 2> for the below XML coding.
<root> <H1><title>Heading 2</title> <text>Sample text Sample text Sample text Sample text Sample t +ext Sample text Sample text Sample text Sample text </text> <text>Sample text Sample text Sample text Sample text Sample t +ext Sample text Sample text Sample text Sample text </text> <text>Sample text Sample text Sample text Sample text Sample t +ext Sample text Sample text Sample text Sample text </text> <H2><title>Heading 2</title> <text>Sample text Sample text Sample text Sample text Samp +le text Sample text Sample text Sample text Sample text </text> <text>Sample text Sample text Sample text Sample text Samp +le text Sample text Sample text Sample text Sample text </text> <text>Sample text Sample text Sample text Sample text Samp +le text Sample text Sample text Sample text Sample text </text> </H2> </H1> </root>
Any help would be greatly appreciated. Thanks in advance. Sandy

Comment on Replacing XML Tag name with another
Select or Download Code
Re: Replacing XML Tag name with another
by Ratazong (Prior) on Sep 27, 2011 at 11:43 UTC

    So where do you encounter problems?

    • in step 1: opening the file?
    • in step 2: reading the file line-by-line?
    • in step 3: doing the replacements (using a regex-substitution s///g)?
    • in step 4: writing the result?

    update: hint: For such simple replacements, treating the XML-file as a text-file is probably fine. However if you want more complex transformations, a using a CPAN-module (e.g. XML::Simple) is a much better choice.

Re: Replacing XML Tag name with another
by dHarry (Abbot) on Sep 27, 2011 at 12:20 UTC

    I advice using a proper XML parser! They come in various flavors. XML::Simple jumps to mind. Or maybe even better XML::Twig as it probably doesn't end with replacing tags and you need to do other things with your xml file.

    I advice against parsing the file line-by-line and apply some regexp. This type of solution easily breaks. Using a decent parser gives you a more "future proof" solution.

Re: Replacing XML Tag name with another
by mirod (Canon) on Sep 27, 2011 at 12:20 UTC

    If you replace 'H1' by 'Heading 1', the resulting data will no longer be well-formed XML, tag names cannot include spaces, so are you sure you want to do this?

    Looking at your data, I am also not sure if the names are right. It looks to me like you have nested sections, unless you have quite complex headings, that include a title, text and sub-headings with more text. So maybe something like section1 and section2 would be more appropriate.

    So with a proper XML parser (I am partial to XML::Twig), which makes the solution easier to write and more robust:

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { H1 => sub { $_->set_tag( 'section1' +); }, H2 => sub { $_->set_tag( 'section2' +); }, }, pretty_print => 'indented', ) ->parsefile( $ARGV[0]) ->print;
Re: Replacing XML Tag name with another
by j0se (Pilgrim) on Sep 27, 2011 at 12:21 UTC
    Developing Ratazong's comment:
    #!/usr/bin/perl use strict; use warnings; # file given as command line argument my $file = shift @ARGV; die "Usage: $0 <file.xml>\n" unless $file; # step 1: open the file open FH, "<", $file or die "Can't open '$file': $!\n"; # step 2: read the file line-by-line while (<FH>) { # step 3: do the replacements s/<(\/?)H([12])>/<$1Heading $2>/g; # step 4: write the results the STDOUT print; } close FH;
    Cheers, j
Re: Replacing XML Tag name with another
by MidLifeXis (Prior) on Sep 27, 2011 at 12:28 UTC

    PerlMonks is not a code writing service. You will get more help <update>(although it looks like you have been provided with solutions as I was writing this)</update> if you show what you have tried, problems you experience, errors or warnings that you receive, desired output, and actual output.

    Like other educational endeavors in life, you will get more out of it if you put more into it. In the process of describing your issue, you may stumble across some questions or ideas that give you an insight to help you resolve the problem on your own. This is not to say that you won't get help here, just that experience can be an excellent teacher. Walking through problem is a skill that can prove to be very valuable in other areas in life.

    --MidLifeXis

Re: Replacing XML Tag name with another
by choroba (Abbot) on Sep 27, 2011 at 12:45 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928068]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-09-17 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (94 votes), past polls