in reply to Re^2: replace the first tag
in thread replace the first tag
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>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |