Since various other solutions have been posted, and you haven't been exactly clear in your requirements, here I use a parser, Mojo::DOM, and Mojo::File for reading/writing. There's more than one way to do it, other parsers exist (XML::Twig etc)...

source.xml

<inline-formula id="ieqn-1"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula> <inline-formula id="ieqn-2"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula> <inline-formula id="ieqn-3"><alternatives><mml:math display="inline"><mml:mi>&#964;</mml:mi></mml:math></alternatives></in +line-formula>

1226654.pl

#!/usr/bin/perl use strict; use warnings; use Mojo::DOM; use Mojo::File; # read the xml file my $source = Mojo::File->new( 'source.xml' ); my $xml = $source->slurp; # use Mojo::Dom to parse the XML my $dom = Mojo::DOM->new->xml(1)->parse( $xml ); # for each inline-formula id in the file for my $e ( $dom->find('inline-formula[id]')->each ){ # create file named value_of_id.mml my $file = "$e->{id}.mml"; my $path = Mojo::File->new( $file ); # write contents to file $path->spurt( $e->to_string ); }

output

-rw-rw-r-- 1 marto marto 131 Dec 4 10:17 ieqn-3.mml -rw-rw-r-- 1 marto marto 131 Dec 4 10:17 ieqn-2.mml -rw-rw-r-- 1 marto marto 131 Dec 4 10:17 ieqn-1.mml marto@Shemp:~/code/perlmonks$ cat ieqn-1.mml <inline-formula id="ieqn-1"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula> marto@Shemp:~/code/perlmonks$ cat ieqn-2.mml <inline-formula id="ieqn-2"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula>

In reply to Re: extract tag form xml file by marto
in thread extract tag form xml file by Murugan

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.