Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Read the attribute from xml file

by GotToBTru (Prior)
on Jul 20, 2015 at 14:55 UTC ( [id://1135453]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Read the attribute from xml file
in thread Read the attribute from xml file

First suggestion: use XML::Twig instead of XML::Simple. There are some good tutorials out there.

XML::Simple reads in the XML data into a Perl data structure, but doesn't seem to give much help in working with it. If you use Data::Dumper you can print out the data structure and go from there. You'll get good with hash references!

In my own exploration, I found the following produced a more useful data structure than the default:

my $xml = XML::Simple; my $data = $xml->XMLin($filename, KeyAttr=>['identifier']);

In that way, the key to the hash containing the attributes is the identifier.

Dum Spiro Spero

Replies are listed 'Best First'.
Re^4: Read the attribute from xml file
by bhushanQA (Sexton) on Jul 21, 2015 at 10:00 UTC
    Hi.. I have written my own code, right now I am able to print the whole file:
    #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper ; my $xml_Event = XMLin('EventDescription.xml', KeyAttr =>{Event => 'nam +e'}); my $Event = $xml_Event->{Event}; print Dumper($Event);
    Now I want to print name and required field, How will I do that ?? all content stored in variable.
      'use of this module in new code is discouraged'. Did you read the docs? Dumping the file you read isn't what you want.

      I'm not here to write programs for you. Check out this for help in working with references.

      $VAR1 = { 'name' => 'Constants', 'attribute' => [ { 'identifier' => '00', 'required' => ' true ', 'name' => ' session_id ', 'type' => ' string ', 'ilog' => { 'visible' => ' false ', 'alternate' => ' session id in st +ring format ' } }, { 'identifier' => '01', 'required' => ' false ', 'type' => ' int ', 'name' => ' volume ', 'ilog' => { 'alternate' => ' the volume ', 'visible' => ' false ' } } ], 'properties' => { 'visible' => ' false ' }, 'serviceKey' => '0', 'version' => '0', 'ui-visible' => '0', 'id' => '0', 'rateTableName' => '' };

      In your program, $Event is a hash reference. The data you want is under the attribute key. So you'd start with $Event->{attribute}. That is an array ref. Each element of that array is ANOTHER hash ref. 'name' and 'required' are keys in that hash.

      What I usually do in this situation is run the program in the debugger. Then I can incrementally build the expression that gets to the data I need.

      If you're thinking 'wow, this is alot of work', well yeah it is. That's why you should invest the time to learn a real XML module instead of XML::Simple.

      Dum Spiro Spero

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found