Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 9th, 2011, 12:27 PM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help with the perl script.

Hi everyone,
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.


Code:
<?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/engineservice.xml"/>
   <Hardwaremodule Type="motor"   Nr="7" Servicenum="1" Path="/main/motorservice.xml"/> 
   <Hardwaremodule Type="supply" Nr="1" Servicenum="1" Path="/main/supplyservice.xml"/>

</Suppliermodules>

</Service>
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
Code:
#!/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.

Last edited by pavanibiji; December 9th, 2011 at 12:33 PM.
Reply With Quote
  #2 (permalink)  
Old December 10th, 2011, 07:46 AM
Authorized User
Points: 93, Level: 1
Points: 93, Level: 1 Points: 93, Level: 1 Points: 93, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2009
Location: Moldova
Posts: 23
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Use XPath. See findnodes here: http://search.cpan.org/perldoc?XML::LibXML::Node
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP can't exec perl script frankm9639 Classic ASP Basics 0 November 4th, 2009 04:10 PM
batch file for perl script arunsand ASP.NET 2.0 Professional 2 March 9th, 2009 06:50 PM
optimise it or speed up execution time perl script crmpicco Perl 2 November 22nd, 2006 08:31 AM
connecting to XML document with Perl script crmpicco XML 0 October 18th, 2006 01:00 PM
How to Perl script in Windows 2000 Pro? mittalpa Flash (all versions) 1 July 9th, 2003 04:12 AM



All times are GMT -4. The time now is 09:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.