I think what you are looking for is the Objects style of XML::Parser. I have not used it as it generates a structure I (and many others!) find quite ugly.
Try this to figure out what yuo can get from it:
#!/bin/perl -w
use strict;
use XML::Parser;
use Data::Denter;
use Text::Iconv;
my $p= new XML::Parser( Style=> 'Objects');
my $doc= $p->parse( \*STDIN);
print Denter( $doc);
I agree with andreychek and I would advise you to use either XML::Simple, which takes the structure generated by the Tree style of XML::Parser and simplifies it a _lot_ (and as epoptai mentions, don't forget to use the force(array option) Luke!
One important drawback of XML::Simple is that it does not deal with mixed content (<p>this is <b>mixed</b> content</p>, text and elements are mixed directly within the p element). If your data includes mixed content (or if it might include it one day) you can have a look at XML::Path (nice, solid module, well supported), XML::DOM (ugly, dangerous and not too well-supported but the DOM is a W3C standard) or of course XML::Twig (which I wrote, so I like it!). All of those modules will generate a tree structure from the XML and allow you to navigate and update it.
There is quite a lot of information about those on this site, just do a SuperSearch on XML and you'll get more information that you might ever want ;--)
If you are looking for pure speed you can also use XML::Parser directly and build your own structure, look at the XML::Parser Tutorial for help.
There is also a tutorial on Perl and XML on my web site at www.xmltwig.com (the DNS info for it might be corrupted at the moment so you can try 193.251.86.24 instead).
Another important note is that since your input is in ISO-8859-1 you might want to get the ouptut in the same character set. XML::Parser (and all of the modules based on it) will convert it to UTF-8 (which is an encoding for the Unicode character set...). Your best bet here is to use Text::Iconv if the iconv library is available on your system (it should be, I think you can get it even on windows):
#!/bin/perl -w
use strict;
use Text::Iconv;
use XML::Simple;
my $converter= Text::Iconv->new("utf8", "ISO-8859-1")
or die "cannot generate the converter";
# note that if you want the text with XML::Parser
# Objects style you will need to get
# $doc->[0]->{Kids}->[0]->{Text}
my $doc= XMLin( \*DATA);
my $latin1_text= $converter->convert( $doc);
print "text: $latin1_text (was $doc)\n";
__DATA__
<?xml version="1.0" encoding="ISO-8859-1"?>
<doc>español</doc>
|