Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Parsing XML file for more than 1 child element with attributes

by Cody Pendant (Prior)
on Feb 06, 2008 at 11:21 UTC ( #666522=note: print w/replies, xml ) Need Help??


in reply to Parsing XML file for more than 1 child element with attributes

You could do it this way (XML::Simple creates a hash of hashes because the nodes have an attribute called "name", I guess):
foreach $node ( keys( %{ $tree->{report}->{node} } ) ) { print "$node:values: $tree->{report}->{node}->{$node}->{values}\n"; }
But you'd be better off adding KeyAttr to your new() call:
my $simple = XML::Simple -> new (KeyAttr => 'node');
Because that way you will get an array of nodes
$tree = { 'report' => { 'ip' => '1.1.1.1', 'id' => '0', 'node' => [ { 'mid' => 'machine1', 'time' => '110, 120, 130, 140, 150, 160', 'name' => 'Winxp', 'values' => '1, 2, 3, 4, 5, 6' }, { 'mid' => 'machine2', 'time' => '110, 120, 130, 140, 150, 160', 'name' => 'Win2003', 'values' => '1, 2, 3, 4, 5, 6' } ] } };
Hint -- use Data::Dumper to look at the tree.


Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re^2: Parsing XML file for more than 1 child element with attributes
by Anonymous Monk on Feb 06, 2008 at 12:25 UTC
    Thanks. Using Data Dumper i was able to see the tree structure and this is the structure that i was looking for. I tried the for loop that you have provided "foreach $node ( keys( %{ $tree->{report}->{node} } ) ) { print "$node:values: $tree->{report}->{node}->{$node}->{values}\n"; }" But here what i observe is that the keys are obtained as mid, time name and values. I am trying to accomplish something like " if the mid is 'machine1' and name is 'Winxp' then i would like to get the 'time' and 'values'. " Similary i will loop across many mid and names. Since i already know the mid and name, it is easy. But the problem is how to construct code for this task ?
      Well, I think the reason nobody's helping you is, you haven't shown any of your own code yet.

      If you understand how

      foreach $node ( keys( %{ $tree->{report}->{node} } ) ) { print "$node:VALUES: $tree->{report}->{node}->{$node}->{values}\n"; }
      works, then you should be able to just add an "if" to my code to get what you want. If you don't, or if you don't even know how to do "if", in Perl, you'd better say so.


      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2020-07-08 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?