Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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:

xmlrefs.pl 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:).


In reply to Re: easy way of parsing XML by BrowserUk
in thread easy way of parsing XML by amir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2021-08-06 02:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (44 votes). Check out past polls.

    Notices?