Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: parsing multi level XML with XML::Simple

by dasgar (Priest)
on Jan 26, 2012 at 05:25 UTC ( [id://950028]=note: print w/replies, xml ) Need Help??


in reply to parsing multi level XML with XML::Simple

When using XML::Simple, it's usually very helpful to use something like Data::Dumper to help understand the structure of the data. From my personal experience, attributes are usually hash keys and multiple tags at a level are usually arrays.

Here's the code that I came up with to parse your XML data and run it through Data::Dumper.

use strict; use warnings; use XML::Simple; use Data::Dumper; my $file = "data.xml"; my $xml = XMLin($file); print Dumper($xml);

And here's the output.

$VAR1 = { 'levels' => { 'level' => { 'c' => { 'target' => '22.1', 'level' => { 'g' => { 'weight' => +'30.8', 'study' => { ' +p' => { + 'weight' => '75', + 'id' => '50' + }, ' +q' => { + 'weight' => '25', + 'id' => '70' + } } }, 'd' => { 'level' => { ' +e' => { + 'weight' => '44.4', + 'study' => { + 'l' => { + 'weight' => '50', + 'id' => '34' + }, + 'm' => { + 'weight' => '50', + 'id' => '27' + } + } + }, ' +f' => { + 'weight' => '55.6', + 'study' => { + 'n' => { + 'weight' => '60', + 'id' => '25' + }, + 'o' => { + 'weight' => '40', + 'id' => '38' + } + } + } }, 'weight' => +'69.2' } }, 'weight' => '13' }, 'h' => { 'target' => '19.0', 'weight' => '22', 'study' => { 'r' => { 'weight' => +'90.9', 'id' => '40' }, 's' => { 'weight' => +'9.1', 'id' => '22' } } }, 'b' => { 'target' => '35.1', 'weight' => '50', 'study' => { 'j' => { 'weight' => +'70', 'id' => '36' }, 'i' => { 'weight' => +'30', 'id' => '32' } } } }, 'target' => '29.6', 'name' => 'a', 'study' => { 'weight' => '15', 'name' => 'k', 'id' => '37' } }, 'study' => { 'u' => { 'spec' => 'p0f7=1', 'weight' => '0', 'id' => '19' }, 'v' => { 'weight' => '0', 'id' => '62' }, 't' => { 'spec' => 'p0f7=2', 'weight' => '0', 'id' => '19' } } };

That output should help you figure out how to write your code to traverse the data structure. If that's not a data structure that you're liking, you can check out the available options for the XMLin function or you might need to check out other XML parsing modules. Since I personally have only used XML::Simple, I can't really recommend any other modules to try.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://950028]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found