• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1546
  • Last Modified:

Perl/Shell Script to edit XML Files

Hi Experts,
 I would appreciate some perl/shell scripts which I can use to edit an XML file to insert the data which i have in a flat file.

For ex: Here is a sample XML file

<oim-data resource="Siebel Resource Object">

<attribute name="IT Resource Type">SIEBEL IT Resource </attribute>


<child-record table="UD_SIEBEL_R">

 <attribute name="Responsibility">SNI - Broker</attribute>
</child-record>
</oim-data>
In the above code, i would want to edit the tag

<attribute name="Responsibility">SNI - Broker</attribute>

and generate multiple XML Files with the data i have from the flat file, the sample flat file as follows

Flat File:
SNI - Inside Sales Leader/Exec
SNI - Collections Agent/Credit Tech
SNI - Data Steward/Info Admin
SNI - Campaign Administrator
SNI - Sales Team Administrator
SNI - Inside Sales User

and would want to name the resulting XML file to as SNI - Inside Sales Leader/Exec.xml and the output XML should look like

<oim-data resource="Siebel Resource Object">

<attribute name="IT Resource Type">SIEBEL IT Resource </attribute>


<child-record table="UD_SIEBEL_R">

 <attribute name="Responsibility">SNI - Inside Sales Leader/Exec</attribute>
</child-record>
</oim-data>


Thanks in advance
0
itsme_asif
Asked:
itsme_asif
  • 3
  • 3
1 Solution
 
Adam314Commented:
Note that there are some characters in your new names that cannot be used in a filename, such as /.  And, although allowed, spaces are not usually wanted in a filename.  Here is a script that does what you want, replacing the invalid characters with _ in the filename.
#!/usr/bin/perl
use strict;
use warnings;
 
#NOTE: replace flat.txt with the name of your flat file
open(my $in, "<flat.txt") or die "Flat: $!\n";
my @flat = <$in>;
chomp(@flat);
 
local $/;
#NOTE: replace test1.xml with the name of your original xml
open($in, "<test1.xml") or die "xml: $!\n";
my $xml = <$in>;
close($in);
 
foreach (@flat) {
	next unless /\S/;  #Skip blank lines
	my $name=$_;
	$name =~ s/[^\w]/_/g;
	
	my $newxml=$xml;
	$newxml =~ s/(<attribute name="Responsibility">).*(<\/attribute>)/$1$_$2/;
	open(my $out, ">$name.xml") or die "out: $!\n";
	print $out $newxml;
	close($out);
}

Open in new window

0
 
itsme_asifAuthor Commented:
Thanks Adam, works like a charm, As far as the file name , even the lines with '/' are getting replaced by an '_', for ex: SNI Broker is getting replaced as SNI___Broker_ , actually i would like to  supply the file names by a different flat file with no '/', can you please make the change for that.
0
 
Adam314Commented:
Like I said, spaces are not normally wanted in filenames, so the script removes them.  To allow spaces, change line 19 to this:
    $name =~ s/[^\w ]/_/g;
(notice the added space after w)

If you want to supply filenames:
#NOTE: replace flat.txt with the name of your flat file
open(my $in, "<flat.txt") or die "Flat: $!\n";
my @flat = <$in>;
chomp(@flat);
close($in);
 
#NOTE: replaces names.txt with the name of the file containing filenames
open($in, "<names.txt") or die "Names: $!\n";
my @names=<$in>;
chomp(@names);
close($in);
 
local $/;
#NOTE: replace test1.xml with the name of your original xml
open($in, "<test1.xml") or die "xml: $!\n";
my $xml = <$in>;
close($in);
 
for(my $i=0; $i<=$#flat; $i++) {
    next unless $flat[$i] =~ /\S/;  #Skip blank lines
    
    my $newxml=$xml;
    $newxml =~ s/(<attribute name="Responsibility">).*(<\/attribute>)/$1$_$2/;
    open(my $out, ">$names[$i]") or die "out: $!\n";
    print $out $newxml;
    close($out);
}

Open in new window

0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
itsme_asifAuthor Commented:
Hi Adam, I tried your above script but i am getting
Use of uninitialized value $_ in concatenation (.) or string on the line

$newxml =~ s/(<attribute name="Group">).*(<\/attribute>)/$1$_$2/;

Here's my flat.txt (names.txt is also the same,its just a copy of flat.txt)

BIABuinessGroupSEC
BIAFINAdhocUser
BIAFINPayDB
BIAFINPayLtd1DB
BIAFINRecvDB
BIAFINRecvLtd1DB
BIAHRAdhocUser
BIAHRPerformanceDB
BIAHRRetentionDB
BIAHRWorkProfileDB
BIAOperatingUnitSEC
BIAPerfMgmntDB
BIAProfitDB
BIAPurchTransDB
BIARlcMgrLvlSEC
BIASTMAdhocUser
BIASellServicesDB
BIASellerLvlSEC
BIBIPWebServices
#!/usr/bin/perl
use strict;
use warnings;
 
#NOTE: replace flat.txt with the name of your flat file
open(my $in, "<flat.txt") or die "Flat: $!\n";
my @flat = <$in>;
chomp(@flat);
close($in);
 
#NOTE: replaces names.txt with the name of the file containing filenames
open($in, "<names.txt") or die "Names: $!\n";
my @names=<$in>;
chomp(@names);
close($in);
 
local $/;
#NOTE: replace test1.xml with the name of your original xml
open($in, "<test1.xml") or die "xml: $!\n";
my $xml = <$in>;
close($in);
 
for(my $i=0; $i<=$#flat; $i++) {
    next unless $flat[$i] =~ /\S/;  #Skip blank lines
    
    my $newxml=$xml;
    $newxml =~ s/(<attribute name="Group">).*(<\/attribute>)/$1$_$2/;
    open(my $out, ">$names[$i]") or die "out: $!\n";
    print $out $newxml;
    close($out);
}

Open in new window

0
 
itsme_asifAuthor Commented:
Btw this is my test1.xml
<oim-data resource="OBIEE Account">
 
<attribute name="Domain">OBIEE</attribute>
 
 
<child-record table="UD_OBEGRC_P">
 
 <attribute name="Group">OTMEXTCUSTOMERREADONLY</attribute>
</child-record>
 
</oim-data>

Open in new window

0
 
Adam314Commented:
$newxml =~ s/(<attribute name="Group">).*(<\/attribute>)/$1$flat[$i]$2/;
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now