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>

In reply to Re: XML Transaction - random access. by mirod
in thread XML Transaction - random access. by tomazos

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.