Hi monks, I have Xml files in a folder, I need to extract some attribute values form xml files and store in a hash. My xml file look like this.

<?xml version="1.0" encoding="UTF-8"?> <Servicelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi +:noNamespaceSchemaLocation="file:///files/service.xsd"> <Service Num="B7a" Name="temperature sensor"> <Des>It delivers actual temperature in the form ov Volts</Des> <Customermodules> <Softwaremodule Service="ADC" Path="/main/ADCservice.xml"/> </Customermodules> <Suppliermodules> <Softwaremodule Service="input" Path="/main/inputservice.xml"/> <Softwaremodule Service="signal" Path="/main/signalservice.xml"/> <Hardwaremodule Type="engine" Nr="18" Servicenum="1" Path="/main/e +ngineservice.xml"/> <Hardwaremodule Type="motor" Nr="7" Servicenum="1" Path="/main/mo +torservice.xml"/> <Hardwaremodule Type="supply" Nr="1" Servicenum="1" Path="/main/sup +plyservice.xml"/> </Suppliermodules> </Service> </Servicelist>

In hash service num attribute is the key and Customermodules ,Suppliermodules attributes are the values. for example In the xml file B7a is the key and its Customermodules ,Suppliermodules attribute values are the values to the key, like that.

I tried like this but Its not working

#!/usr/bin/perl use warnings; use strict; use XML::LibXML; use Carp; use File::Find; use File::Spec::Functions qw( canonpath ); use XML::LibXML::Reader; my %hash; my @ARGV ="C:/Main"; die "Need directories\n" unless @ARGV; find( sub { return unless ( /(_service\.xml)$/ and -f ); extract_information(); return; }, @ARGV ); sub extract_information { my ($path $hash) = $_; if( my $reader = XML::LibXML::Reader->new( location => $path )){ while ( $reader->nextElement( 'Service' )) { my $elem = $reader->getAttribute( 'Id'); $reader->nextElement( 'Customermodules' ); my $elem1 = $reader->getAttribute( 'Service'); $reader->nextElement( 'Suppliermodules' ); my $elem2 = $reader->getAttribute('Service'); $hash->{$elem} = $elem1; push @{$hash{$elem}}, '$elem2'; } } return; } print my $num=keys%hash;

Can any one help me with the script, that extract attributes from xml file and store in a hash, I need to get all attributes in Suppliermodules( type, nr), no need of path attribute at any where. Please help me with this problem,i am learning perl scripting language.

Thanks in advance


In reply to how to get attribute values and store in a hash. by veerubiji

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.