Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

easy way of parsing XML

by amir (Sexton)
on Jun 29, 2002 at 08:09 UTC ( [id://178217]=perlquestion: print w/replies, xml ) Need Help??

amir has asked for the wisdom of the Perl Monks concerning the following question:

I am relatively new to XML, and want to parse an XML file generated (exported) by MS Access:
<ItemList> <DlrItemNum>1</DlrItemNum> <Title>Table</Title> <Dimensions>3.56 x 5.18m</Dimensions> <Width>3.56</Width> <Length>5.18</Length> <Circa>Early 20th Century</Circa> <Description>A wonderful table.</Description> <Origin>Canada</Origin> <Category1>General</Category1> <Category2>Old Products</Category2> </ItemList>
Which of the Perl XML modules should I use? The above XML structure is repeated for about 500 other products, would XML::Simple suffice? Any bits of code would be appreciated. :)

Replies are listed 'Best First'.
Re: easy way of parsing XML
by BrowserUk (Patriarch) on Jun 29, 2002 at 17:45 UTC

    This is a little program I wrote to help me A) get to grips with Perl references. B) Understand the stuctures returned by XMLIn.

    It may give you a place to start.

    #!e:/Perl/bin/Perl.exe -w use strict; use warnings; use XML::Simple; my $file = shift or die <<USAGE; Usage: $0 <xmlfile> [label] [0|1] Where; <xmlfile> is the path/name of the .xml file to parse. [label] is the base of the references as output (default:'\$xml->' +). [0|1] indicate whether to append the contents of the tags. (defaul +t:0 (no)). Parses an XML file using XML::Simple and (if the doc is well-forme +d) outputs the perl references to access the elements of the structur +e, optionally appending the contents of the tags. USAGE my $xml = XMLin( $file, parseropts => [ ErrorContext => 1, ], forcearr +ay => 0, ); sub walk { my ($label, $valuesFlag) = (shift||"xml->", shift||0); my ($output, $tab) = ( "", ". "); foreach my $thing (shift) { if ( ref($thing) eq "HASH" ) { $output .= ( !ref ${$thing}{$_} ) ? "$label\{$_\}" . ( $valuesFlag ? " := ${$thing}{$_}\n" : "\n" ) : walk( $label . "{$_}", $valuesFlag, ${$thing} +{$_} ) foreach ( keys %$thing ); } if ( ref($thing) eq "ARRAY" ) { $output .= ( !ref @{$thing}[$_] ) ? "$label\[$_\] " . ( $valuesFlag ? " := @{$thing}[$_]\n" : "\n" ) : walk( $label . "[$_]", $valuesFlag, @{$thing}[$_ +] ) foreach ( 0.. @{$thing} -1 ); } $output .= " := $thing\n" if ( ref $thing eq "SCALAR" ); } return $output; } (my $label= shift) =~ s/^(.+)+$/$1->/; #print $label . "\n"; print walk $label, shift||0, $xml;
    When I run this on your sample XML using the following command line: C:\test\items.xml "" 1 >itemlist.refs

    where items.xml is your sample XML; "" is a place holder for an optional alternative base tag (I know getopt::std/long!); and the "1" indicates I want to see the contents of the tags as well.

    Gives the following output.

    xml->{Category1} := General xml->{Origin} := Canada xml->{Description} := A wonderful table. xml->{DlrItemNum} := 1 xml->{Category2} := Old Products xml->{Length} := 5.18 xml->{Title} := Table xml->{Width} := 3.56 xml->{Circa} := Early 20th Century xml->{Dimensions} := 3.56 x 5.18m

    I find that I tend to use a label (second param) of the form "#xml->" and append the output to the end of my perl script for easy reference. Specifying 0 (or omitting) the 3 parameter so that you don't get the contents of the tags make cut&paste even easier.

    Update:Corrected SCALER=>SCALAR spelling. (Here, and in my copy of the prog:).

      XML::Simple tries to provide a simple interface but it does assume a knowledge of Perl references. I recommend perlreftut.

      The most common mistake with XML::Simple is to ignore the advice in the docs about the ForceArray and KeyAttr options. Always set ForceArray => 1 if you're not sure, and setting it to an array of element names is probably the best way.

      I also recommend setting KeyAttr => [] unless you know what you want. In the case of the original XML snippet, KeyAttr =>  'DlrItemNum' might be useful.

      I suspect the line that says if ( ref $thing eq "SCALER" ); will never get executed.

      If you're processing big XML files, XML::SAX might be a good answer. XML::SAX::ByRecord from Barrie Slaymaker's XML::SAX::Machines could be very handy once you have your head around SAX.

      But, XML::Twig is possibly the best answer for simple record oriented processing.

      Edit: Sorry, I typed 'always set ForceArray => 0 ...' when I meant 'ForceArray => 1 ...'

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://178217]
Approved by little
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-06-19 00:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.