in reply to Read the attribute from xml file

What do you have so far?

Dum Spiro Spero

Replies are listed 'Best First'.
Re^2: Read the attribute from xml file
by bhushanQA (Sexton) on Jul 20, 2015 at 13:15 UTC
    #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper ; my $xml_Event = XMLin('EventDescription.xml'); warn "$xml_Event->{Event};

      Hello bhushanQA,

      The XML you’ve shown is not valid, as the <Event> element has no closing tag. When that is added, reading the XML is fairly straightforward. I’m not sure exactly what you’re trying to do; this is my best guess:

      #! perl use strict; use warnings; use XML::Simple; my $xml = do { local $/; <DATA>; }; my $xml_Event = XMLin($xml); for (keys %{ $xml_Event->{attribute} }) { printf "'%s' is %srequired\n", $_, $xml_Event->{attribute}{$_}{required} =~ /true/i ? '' : 'no +t '; } __DATA__ <Event name="Constants" id="0" ui-visible="0" version="0" serviceKey=" +0" rateTableName=""> <properties> <visible> false </visible> </properties> <attribute identifier="00"> <name> session_id </name> <type> string </type> <required> true </required> <ilog> <alternate> session id in string format </alternate> <visible> false </visible> </ilog> </attribute> <attribute identifier="01"> <name> volume </name> <type> int </type> <required> false </required> <ilog> <alternate> the volume </alternate> <visible> false </visible> </ilog> </attribute> </Event>

      Output:

      0:40 >perl 1313_SoPW.pl ' volume ' is not required ' session_id ' is required 0:40 >

      But please note the following from the documentation for XML::Simple:

      STATUS OF THIS MODULE

      The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Hahaha with style :)
        while( my( $att, $attref ) = %{ $event->{attribute} }){ print "$att required " if $attref->{required}; }
        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 ??

      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
        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.