Re: XML Parsing 
----- Original Message ----- 
From: "mirod" 
To: 
Sent: Tuesday, June 26, 2007 2:46 AM
Subject: Re: XML Parsing


> On Jun 25, 3:47 am, mick...@frontiernet.net (Mike Blezien) wrote:
>> I need to parse a fairly largeXMLresponse file and would like some 
>> suggestions
>> on whichXMLmodule would work the best. We've been using theXML::Simple
>> module, but I don't think that's the right one for the job. Below is theXML
>> file we need to parse. In particular getting the data from the elements 
>> within
>> the 
>> Would theXML::Twigmodule work better for this ?? Any suggestion would be much
>> appreciated.
>
> Hi Mike,
>
> XML::Twig would probably work fine, you can for example do
> XML::Twig->new( twig_roots => { message => sub { push @messages, $_-
>>text;  $_->purge;}, },)
>               ->parsefile( 'file.xml');
>
> message texts will be in @messages
>
> If you need more information than just the text of the message
> elements, then you can use twig_handlers instead of
> twig_roots, look at the docs for the difference.
>
> That said if the files are the size of your example, that's really
> small, so there should be no need to switch from a module that
> loads the entire file in memory. OTOH if message elements can contain
> mixed content (embedded markup like  for example),
> then that would be a reason to avoid XML::Simple.
>
> I hope that helps.
>
> --
> mirod

XML Snip:
       
 ..... 
 ..... 


When using the 'twig_handlers' which may have multiple child elements, IE:

my $twig = new XML::Twig(twig_handlers => {raiserisklevel =>\\&print_message});
$twig->parsefile('/path/to/xml_file');

sub print_message {
my($t,$elt) = @_;

my $message =  $elt->first_child('message')->text;

print qq~Message: $message

 $t->purge;
}

Will this print all the text within the message elements??

Mike 

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


