Dear brethren,

I have to add XML markup to a text file. E.g. I must markup dates and numbers. Since dates often contain numbers (as in 4/2/2001), my approach would be to first find all dates, mark them up (or is it markup them?), and then scan the remaining (non-marked up) text for numbers.

Specifically, I was thinking of using XML::DOM::Node to first create a node containing all the text and then add nodes for dates and numbers as I find them. In the code snippet below, I assume that I have functions findDate and findNumber that return the text before the date/number, the date/number itself, and the text after it (or undef if there is no date in the text). So I end up with the following code:

#createMarkup creates markup for dates and numbers in the given text, #e.g. 'On Oct. 21, the Dow Jones rose to 10043 points' should become #'<mytxt>On <date>Oct. 21</date>, the Dow Jones rose to <number>10043< +/number> points</mytxt> sub createMarkup { my ($text, $doc) = @_; #create parent node my $node = new XML::DOM::Element ($doc, 'mytxt'); #markup dates my $textNode = $node->addText ($text); markupElement ($textNode, $node, \&findDate, 'date'); #markup numbers foreach my $child ($node->getChildNodes ()) { next unless $child->isTextNode (); my $frag = $child->getNodeValue (); markupElement ($child, $node, \&findNumber, 'number'); } return $node; } sub markupElement { my ($textNode, $parent, $rFindFunc, $elemName) = @_; my $doc = $parent->getOwnerDocument (); die unless $textNode->isTextNode (); my $nextNode = $textNode->getNextSibling (); my $text = $textNode->getValue (); while (my ($before, $elem, $after) = &$rFindFunc ($text)) { $textNode->setValue ($before); my $elemNode = new XML::DOM::Element ($doc, $elemName); $elemNode->setValue ($elem); $parent->insertBefore ($elemNode, $nextNode); $textNode = $doc->createTextNode ($after); $parent->insertBefore ($textNode, $nextNode); $text = $after; } }
Is there a more elegant way to do this? And is XML::DOM::Node and subclasses the right thing to use? Or what should I do? In reality I have about 20 different tags to add to the text, so proposals should not rely on finding just two entities as shown in the example.

pike


In reply to Building an XML File from text by pike

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.