veerubiji has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to get attribute values and store in a hash.
by kennethk (Abbot) on Dec 09, 2011 at 19:05 UTC | |
by veerubiji (Sexton) on Dec 13, 2011 at 16:02 UTC | |
by kennethk (Abbot) on Dec 14, 2011 at 19:35 UTC | |
|
Re: how to get attribute values and store in a hash.
by choroba (Cardinal) on Dec 10, 2011 at 00:28 UTC | |
|
Re: how to get attribute values and store in a hash.
by Jenda (Abbot) on Dec 15, 2011 at 09:01 UTC | |
|
Re: how to get attribute values and store in a hash.
by locked_user sundialsvc4 (Abbot) on Dec 10, 2011 at 11:40 UTC | |
|
Re: how to get attribute values and store in a hash.
by choroba (Cardinal) on Dec 14, 2011 at 23:58 UTC |