----- Original Message -----
From: "mirod" <[EMAIL PROTECTED]>
To: <beginners@perl.org>
Sent: Tuesday, June 26, 2007 2:46 AM
Subject: Re: XML Parsing
On Jun 25, 3:47 am, [EMAIL PROTECTED] (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 <message></message>
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 <b> for example),
then that would be a reason to avoid XML::Simple.
I hope that helps.
--
mirod
XML Snip:
<raiserisklevel>
<message> ..... </message>
<message> ..... </message>
</raiserisklevel>
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/