Hi, I am trying to parse the inner loop elements of the attached input xml elements. The below code doesn't retrieve the inner loop(<property>) elements if the properties tag contains more than one item. Will you please point the error and correct me. Please find the attached input xml file. Thanks.
use strict; use XML::Simple; use Data::Dumper; die "Usage: perl $0 path output_file\n" unless @ARGV == 2; my $path = shift; my $output_file = shift; my ($name,$url,$value,$password,$user,$prop_name,$string); opendir (DIR, "$path") or die "Can't open the dir: $!\n"; my @files = grep (/\.xml$/, readdir(DIR)); open(CSV, '>', "$output_file") or die "Can't open the file:$!\n"; my $header = "Name,Data Source,URL,Password\n"; print CSV $header; foreach my $file(@files) { print "Processing the file:$file\n"; ($name,$url,$value,$password) = undef; my $file_path = $path .'/'. $file; my $jdbc_data_source = XMLin($file_path); $name = "$jdbc_data_source->{'name'}"; $url = "$jdbc_data_source->{'jdbc-driver-params'}->{'url'}"; $password = "$jdbc_data_source->{'jdbc-driver-params'}->{'password-encrypted'}\n"; if (ref($jdbc_data_source->{'jdbc-driver-params'}->{'properties'}) =~ /ARRAY/) { #print Dumper($jdbc_data_source); foreach my $property (@{$jdbc_data_source->{'jdbc-driver-params'}->{'properties'}}) { #print Dumper($property); # $prop_name = {$property}->[0]; #$prop_name = {$property}->[1]; #No need to capture the value if pop_name eq user; # next if $prop_name eq 'user'; $string .= "$name,$value,$url,$password\n"; print "$string"; } } else { $prop_name = $jdbc_data_source->{'jdbc-driver-params'}{'properties'}{'property'}{'name'} ; #unless ($prop_name eq 'user'){ $value = $jdbc_data_source->{'jdbc-driver-params'}{'properties'}{'property'}{'value'} ; #} $string .= "$name,$value,$url,$password\n"; } #print "$string"; print CSV $string; }
<?xml version='1.0' encoding='UTF-8'?> <jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdbc-data-source/1.2/jdbc-data-source.xsd"> <name>AllocationImport Data Source</name> <jdbc-driver-params> <url>test url 1</url> <driver-name>name 1</driver-name> <properties> <property> <name>den_alien</name> <value>ben1</value> </property> <property> <name>v$session.program</name> <value>ALIMP-OSB-11G</value> </property> </properties> <properties> <property> <name>user</name> <value>ben_alimp_sb</value> </property> <property> <name>v$session.program</name> <value>ALIMP-OSB-11G</value> </property> </properties> <password-encrypted>{encrypted password 1}=</password-encrypted> </jdbc-driver-params> <jdbc-connection-pool-params> <connection-creation-retry-frequency-seconds>600</connection-creation-retry-frequency-seconds> <test-connections-on-reserve>true</test-connections-on-reserve> <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name> </jdbc-connection-pool-params> <jdbc-data-source-params> <jndi-name>jdbc/AllocationImportServiceDS</jndi-name> <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol> </jdbc-data-source-params> </jdbc-data-source>
-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/