in reply to Rewriting XML::DOM based module as XML::SAX

or for something only a little different it may be worth taking a look at XML::Twig.

The code you present could be mapped easily into almost any XML parsing module. What are the new areas that you want to be able to handle that DOM is a pain for?

I notice that at one point you have a for loop over 'SA_PATIENT' elements and retain data from the last element found which is later saved to @patdemo. Is it intended behaviour that you only retain the last element's data?

By the way you could have trimed the code down to about a dozen lines and included some sample data (keep the data small too) for posting purposes - then people may feel more inclined to post sample solutions using other XML systems (I started, but couldn't be bothered - too much code all the same).


DWIM is Perl's answer to Gödel
  • Comment on Re: Rewriting XML::DOM based module as XML::SAX

Replies are listed 'Best First'.
Re^2: Rewriting XML::DOM based module as XML::SAX
by Cappadonna3030 (Sexton) on Jun 12, 2006 at 14:09 UTC

    Well, there are two really. First, every XML file will have the same general structure but not the same information. For example, the XML file A may have a social security number while B doesn't have one. Or, study C may contain EKG readings that don't show up in A or B. Using DOM, I would have to build a level of intelligence and redundancy to ensure that the script doesn't crap out if it doesn't see certain information. From what I've read, the event driven model is more appropriate, since I getting data fed from a another server.

    the other is speed. On my local workstation, the script runs fine. But this module will be part of a (slightly) larger automated script for a hospital in which multiple patients (XML files) will input at the same time. Like most patient care systems, I can't afford for this thing to crash b/c of memory hogging.

Re^2: Rewriting XML::DOM based module as XML::SAX
by Cappadonna3030 (Sexton) on Jun 12, 2006 at 16:44 UTC
    Okay:

    Here is the code:

    sub CamPatData { my $obj = shift; Date_Init("TZ=EST"); if (@_) { my $file = shift; my $parser = XML::DOM::Parser->new(); my $doc = $parser->parsefile($file); my (@patdemo); my ($ln, $fn, $sex, $dob, $htu, $htn, $wtu, $wtn, $patnum, $pa +tient); #Get Patient Data foreach $patient($doc->getElementsByTagName('SA_PATIENT')) { $ln = $patient->getElementsByTagName('MY_LAST_NAME')->item +(0)->getFirstChild->getNodeValue; $fn = $patient->getElementsByTagName('MY_FIRST_NAME')->ite +m(0)->getFirstChild->getNodeValue; $dob = $patient->getElementsByTagName('MY_BIRTH_DATE')->it +em(0)->getFirstChild->getNodeValue; $sex = $patient->getElementsByTagName('MY_GENDER')->item(0 +)->getFirstChild->getNodeValue; $dob = $patient->getElementsByTagName('MY_BIRTH_DATE')->it +em(0)->getFirstChild->getNodeValue; } foreach $patient($doc->getElementsByTagName('CathStudy')) { $htn = $patient->getElementsByTagName('PatHeight')->item(0 +)->getFirstChild->getNodeValue; $htu = $patient->getElementsByTagName('PatHeight')->item(0 +)->getAttribute('Units'); $wtn = $patient->getElementsByTagName('PatWeight')->item(0 +)->getFirstChild->getNodeValue; $wtu = $patient->getElementsByTagName('PatWeight')->item(0 +)->getAttribute('Units'); $patnum = $patient->getElementsByTagName('StudyID')->item( +0)->getFirstChild->getNodeValue; } #Rounding Off $wtn = ceil($wtn); $htn = ceil($htn); my $date = ParseDate($dob); $dob = UnixDate($date,"%m/%d/%Y"); @patdemo = ($ln, $fn, $dob, $sex, $htn, $htu, $wtn, $wtu, $pat +num); }

    Here is the Sample Data:

    </CathStudy> <SA_PATIENT> <MY_PATIENT_ID>XXXXX</MY_PATIENT_ID> <MY_BIRTH_DATE>123-45-6789</MY_BIRTH_DATE> <MY_FIRST_NAME>BERT</MY_FIRST_NAME> <MY_GENDER>M</MY_GENDER> <MY_LAST_NAME>PUPPET</MY_LAST_NAME> <Address>123 SESAME ST</Address> <City>NEW YORK</City> <State>NY</State> <Zip>10110</Zip> <Race>Caucasian</Race> </SA_PATIENT>

    Any suggestions?

      Looks like the simplest way to do this might be to slurp all the data into a hash using XML::Simple then either clean up the hash or extract the data from it into another hash as is appropriate:

      use strict; use warnings; use Data::Dump::Streamer; use XML::Simple; my $str = do {local $/; <DATA>}; my %data = %{XMLin ($str)}; Dump (\%data);