in reply to Converting a Text file to XML
It may be that XML::Writer is helpful. Consider:
#!/usr/lib/perl use strict; use warnings; use XML::Writer; my $out; my $xml = XML::Writer->new(OUTPUT => \$out, DATA_MODE => 1, DATA_INDEN +T => ' '); $xml->xmlDecl(); $xml->startTag('doc'); while (<DATA>) { chomp; next if !length; my ($bibData, $quote, $primary, $sec) = / ^([^"]* "[^"]+"[^"]*) ([^\@]+) \@([^%]+) \%(.*) /x; $xml->startTag('entry'); $xml->dataElement(bib => $bibData); $xml->dataElement(quote => $quote); $xml->dataElement(primary => $primary); $xml->dataElement(sec => $sec); $xml->endTag(); } $xml->endTag(); $xml->end(); print $out; __DATA__ Leyson, Captain Burr. "With or Without Gadgets." Boys' Life. Nov 19 +49. p. 6. "An old-timer knew what he had to do in a jam. He didn't + need hundreds of those gadgets to guide him to safety." @gauge %tri +vial "The Battle Against Baldness." Kiplinger's Personal Finance. Feb 194 +9. "A little home hair-cutter gadget--a comb with a razor attached-- + has zipped its way into fame in recent months. Barbers pooh-pooh it + as a threat, but sales are going strong." @tool %american
Prints:
<?xml version="1.0"?> <doc> <entry> <bib>Leyson, Captain Burr. "With or Without Gadgets." Boys' Li +fe. Nov 1949. p. 6. </bib> <quote>"An old-timer knew what he had to do in a jam. He didn't + need hundreds of those gadgets to guide him to safety." </quote> <primary>gauge </primary> <sec>trivial</sec> </entry> <entry> <bib>"The Battle Against Baldness." Kiplinger's Personal Financ +e. Feb 1949. </bib> <quote>"A little home hair-cutter gadget--a comb with a razor at +tached-- has zipped its way into fame in recent months. Barbers pooh +-pooh it as a threat, but sales are going strong." </quote> <primary>tool </primary> <sec>american</sec> </entry> </doc>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting a Text file to XML
by monk8148n038 (Initiate) on Nov 17, 2011 at 04:32 UTC | |
by graff (Chancellor) on Nov 17, 2011 at 11:03 UTC | |
by monk8148n038 (Initiate) on Nov 17, 2011 at 19:52 UTC | |
by graff (Chancellor) on Nov 18, 2011 at 03:07 UTC |