Connecting Tech Pros Worldwide Forums | Help | Site Map

comibining two xml files thro Perl

Newbie
 
Join Date: Nov 2006
Posts: 5
#1: Nov 15 '06
Hi,
I am very new to Perl scripting and I am trying to modify XML files thro' Perl scipts. I would like to know how can I grab a piece of information (say 3 lines) from one xml file and write it into another xml file?

Any help, greatly appreciated.

Thanks,
N M

Member
 
Join Date: Nov 2006
Posts: 83
#2: Nov 15 '06

re: comibining two xml files thro Perl


"perldoc Tie::File" might be useful.
Newbie
 
Join Date: Nov 2006
Posts: 5
#3: Nov 16 '06

re: comibining two xml files thro Perl


Thanks for ur reply. I guess the Tie is to bind two files, I'm not sure. But, If I want a piece of information from one file to copy into another file, how do I get that? Any thoughts?
Newbie
 
Join Date: Nov 2006
Posts: 5
#4: Nov 16 '06

re: comibining two xml files thro Perl


Is it possible to get a sample code? This will help me a lot ! Thanks [/quote]
Member
 
Join Date: Nov 2006
Posts: 83
#5: Nov 16 '06

re: comibining two xml files thro Perl


Tie::File is for binding an array to a file. The idea to use it was based on assumptions.

Your problem description is vague. It might be easier to understand what the problem is if you post the code you have so far together with example data.
Newbie
 
Join Date: Nov 2006
Posts: 5
#6: Nov 16 '06

re: comibining two xml files thro Perl


I have two XML files. For eg.,
one file will have values like The other file has values like
<Class1> <class1>
<Value1> <Value3>
<Value2> <Value4>
<Value5> </class1>
</Class1> <class2>
<class2> <value3>
<Value1> <value4>
<Value2> </class2>
<Value5>
</class2>

We need the output in the first file to be
<class1>
<vlaue1>
<value2>
<value3>
<value4>
<value5>
</class> where value3 and value4 are from the second file.
Similarly for all the classes.

I am not sure if the above helps you understand the scenario. I am very sorry if I am not so descriptive.

Thanks for your patience.
Member
 
Join Date: Nov 2006
Posts: 83
#7: Nov 17 '06

re: comibining two xml files thro Perl


There are obviously many ways to approach this problem. Here is one:
Expand|Select|Wrap|Line Numbers
  1. open my $f1, '<', 'file1.xml' or die $!;
  2. open my $f2, '<', 'file2.xml' or die $!;
  3.  
  4. my ($file1, $file2);
  5. {
  6.     local $/;
  7.     $file1 = <$f1>;
  8.     $file2 = <$f2>;
  9. }
  10.  
  11. while ( $file1 =~ /<(class\d+)>\n(.+?)<\/\1>/gis ) {
  12.     my $class = $1;
  13.     my $val = $2;
  14.  
  15.     if ( $file2 =~ /<$class>\n(.+?)<\/$class>/is ) {
  16.         $val .= $1;
  17.     }
  18.  
  19.     print "<$class>\n$val</$class>\n";
  20. }
Reply