use strict; use warnings; use XML::LibXML; my ( @docs, $doc, $root ); READ_TAG: while ( my $ident = ) { $ident =~ s/\A\.\.|:.*//gs; chomp( my $value = ); 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") . ".xml"; 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!!!