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

getting required format output from a xml file

by greatshots (Pilgrim)
on Nov 08, 2006 at 09:06 UTC ( [id://582827]=perlquestion: print w/replies, xml ) Need Help??

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

monks,
<level label="SGSN"> <level label="Nokia"> <level label="Card"> <report-def-ref default-operation="unspecified" /> <report-def-ref default-operation="unspecified" /> </level> <level label="Destination Point"> <report-def-ref default-operation="unspecified" /> </level> <level label="IP Interface"> <report-def-ref default-operation="unspecified" /> <report-def-ref default-operation="unspecified" /> </level> <level label="Link Timeslot"> <report-def-ref default-operation="unspecified" /> </level> <level label="Processor"> <report-def-ref default-operation="unspecified"/> </level> <level label="SCP Address"> <report-def-ref default-operation="unspecified"/> </level> <level label="SUB-TREE"> <level label="Three"> <report-def-ref default-operation="unspecified" /> <report-def-ref default-operation="unspecified" /> </level> <level label="Four"> <report-def-ref default-operation="unspecified" /> <report-def-ref default-operation="unspecified" /> </level> </level> </level>
From the above XML file
I would like to get the output like below:- SGSN.Nokia.Card SGSN.Nokia.Destination Point SGSN.Nokia.IP Interface SGSN.Nokia.Link Timeslot SGSN.Nokia.Processor SGSN.Nokia.SCP Address SGSN.SUB-TREE.Three SGSN.SUB-TREE.Four

UPDATE: 1 added more information with the code.
UPDATE: 2 added working code.
#!/usr/bin/perl my $file = "layer.xml"; use XML::Simple; use Data::Dumper; die "Can't find file \"$file\"" unless -f $file; $xml = new XML::Simple(); print "Input file :$file:\n"; # read XML file $data = $xml->XMLin("/apps/inst1/metrica/anthony/3G_SGSN_SGN3/tools/$f +ile"); foreach $keys ( keys %$data ) { next if ( $keys !~ /level/ ); $one = $$data{$keys}{label}; foreach $keys1 ( keys %{$$data{$keys}{level}} ) { $two = $$data{$keys}{level}{label}; foreach $element ( @{$$data{$keys}{level}{$keys1}} ) { print "$one->$two->$$element{label}:\n"; } } }
someone can advise me on making it simpler to traverse the datastructure. I am interested in a tag called 'level' and its 'label'.

Replies are listed 'Best First'.
Re: getting required format output from a xml file
by Corion (Patriarch) on Nov 08, 2006 at 09:08 UTC

    How about reading the XML in, maybe by using XML::Simple, and then outputting the data structure by walking your data structure?

    As you don't show us any hint of the code you've already written, it's kinda hard to give you concrete advice. You don't even show how the output data correlates with the input data. You have to do some work first before we can help you.

Re: getting required format output from a xml file
by Hofmator (Curate) on Nov 08, 2006 at 10:27 UTC
    Here is a solution using XML::Twig. Please feel free to improve on it as I'm new to XML::Twig ...
    use XML::Twig; my $twig= new XML::Twig( start_tag_handlers => { 'level' => \&start_level }, twig_handlers => { 'level' => \&end_level }, ); $twig->parsefile($file); my @levels; sub start_level { push @levels, $_[1]->{att}{label}; print join ('.', @levels), "\n" if @levels == 3 } sub end_level { pop @levels; }

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-18 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found