I'm working on parsing a large XML file of a blog backup and reordering and reformatting the text to be used in InDesign. The XML file is formatted somewhat like this:
... <entry> <type>Post</type> <url>URL</url> <title>The title</title> <content>The blog post goes here</content> </entry> <entry> <type>Comment</type> <in-reference-to>URL</in-reference-to> <title>The title of the comment</title> <content>The content itself</content> </entry> ...
Right now I have my code go through the XML file once, find all the comments, and put them in a temporary file using File::Temp. Here it is in pseudo code
my $tmpComments = File::Temp->new(SUFFIX=>'.txt') or die "File::Temp: +$!\n"; foreach (#Go through the whole xml file) { if ($type eq 'Comment') { # Parse the xml and save the parts as variables print $tmpComments "$posturl~~~$date~~~$author~~~$content\n"; # $posturl holds the <in-reference-to> url } }
I then loop through the xml file again to find and print out all the actual blog posts. On each pass I search the entire temporary file for any comments that start with the post url and add those comments to the post, like so (again in pseudo code):
foreach (#Loop through the xml file again) { print "$title\n"; print "$content\n"; ... seek $tmpComments, 0, 0 or die "Seek $tmpComments failed: $!\n"; #Re +wind temporary comments file while (my $line = <$tmpComments>) { # Split each line, store url as $commentID my @process_comment = split(/~~~/, $line); my $commentID = $process_comment[0]; # If the urls match, add it to the comments variable if ($commentID eq $posturl) { my $commentDate = $process_comment[1]; my $commentAuthor = $process_comment[2]; my $commentBody = $process_comment[3]; $comments.= "$commentDate | $commentAuthor | $commentBody"; } } print $comments; }

It works just fine, but it takes forever with longer xml files, probably since it's uselessly looping through the temporary file so many times.

Is there a more efficient way of doing this?

Is there a way to use a cache to store all the comments indexed by the URL so that when I loop through the blog posts I can look at the cache and pull out the appropriate comments?


In reply to Use temporary file or cache by andrewheiss

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.