Mmmm. I am trying to help you. Regular expressions are a *drag* with XML even when they can work. I would never again waste two minutes of my own time on my own problems that way. I'm sort of amazed at how much time you've managed to get others to waste here on this version.

This is really a much better way to go. I realize it's not easy to wrap your head around this stuff at first so here is your entire solution. I used an attribute in the root node to preserve the DN. You could do it differently if that's somehow troublesome but you should never remove data from a document to "keep" in the file name so...

use strict; use warnings; use XML::LibXML; my ( @docs, $doc, $root ); READ_TAG: while ( my $ident = <DATA> ) { $ident =~ s/\A\.\.|:.*//gs; chomp( my $value = <DATA> ); if ( $ident eq "DN" ) { $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); $root = $doc->createElement("root"); $root->setAttribute("DN", $value); $doc->setDocumentElement( $root ); push @docs, $doc; next READ_TAG; } my $node = $doc->createElement($ident); $node->addChild($doc->createTextNode($value)); if ( $ident eq "p" ) { my $last = $root->lastChild; if ( $last->nodeName eq "con" ) { $last->addChild($node); } else { my $con = $doc->createElement("con"); $root->addChild($con); $con->addChild($node) } } else { $root->addChild($node); } } for my $doc ( @docs ) { my $filename = $doc->getDocumentElement->getAttribute("DN") . ".xm +l"; warn "Writing file: $filename\n"; $doc->toFile( $filename, 1 ); print $doc->serialize(1); # Just to see it. } __DATA__ ..DN: 1 ..id: 000044119 ..DD: Friday, October 30, 2009 ..p: OH HAI! ..p: EXEMELLZ ..p: YUR DOIN IT RONG ..DN: 2 ..id: 000044119 ..DD: Caturday, October 31, 2009 ..p: KANDY! ..p: KANDY!! ..p: KANDY!!!

Output-

Writing file: 1.xml <?xml version="1.0" encoding="UTF-8"?> <root DN="1"> <id>000044119</id> <DD>Friday, October 30, 2009</DD> <con> <p>OH HAI!</p> <p>EXEMELLZ</p> <p>YUR DOIN IT RONG</p> </con> </root> Writing file: 2.xml <?xml version="1.0" encoding="UTF-8"?> <root DN="2"> <id>000044119</id> <DD>Caturday, October 31, 2009</DD> <con> <p>KANDY!</p> <p>KANDY!!</p> <p>KANDY!!!</p> </con> </root>

In reply to Re^3: replace the first tag by Your Mother
in thread replace the first tag by Anonymous Monk

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.