in reply to copy XML elements from one file to another
If you're mostly just copying the contents of several files, perhaps consider not using a module at all. Something along the lines of this code (following some Notes) might do what you want.
Notes:
#!/usr/bin/env perl -l use strict; use warnings; use Inline::Files; my %doc_fh_for = (File0 => \*FILE0, File1 => \*FILE1, File2 => \*FILE2 +); my $out_fh = \*STDOUT; my $key_file = 'File0'; my @feature_files = qw{File1 File2}; print $out_fh '<Key="1234">'; write_xml_content($key_file, $doc_fh_for{$key_file}, $out_fh, ' ' x 4) +; print $out_fh ' <Status="in use"/> <Features>'; for (@feature_files) { write_xml_content($_, $doc_fh_for{$_}, $out_fh, ' ' x 8); } print $out_fh ' </Features> <Other_Status/> </Key>'; sub write_xml_content { my ($file_id, $in_fh, $out_fh, $indent) = @_; while (<$in_fh>) { chomp; if (/^<\/?doc>$/) { s/doc/$file_id/; } else { if (/^<ABC id="(\d+)">$/) { my $id = $1; s/$id/$file_id-$id/; } $_ = ' ' x 4 . $_; } print $out_fh $indent, $_; } }
Inline::Files data:
__FILE0__ <doc> <ABC id="1"> <Feature> <Number>86839</Number> </Feature> </ABC> <ABC id="2"> <Feature> <Number>82783</Number> </Feature> </ABC> </doc> __FILE1__ <doc> <ABC id="1"> <Feature> <Number>86839</Number> </Feature> </ABC> <ABC id="2"> <Feature> <Number>82783</Number> </Feature> </ABC> </doc> __FILE2__ <doc> <ABC id="1"> <Feature> <Number>86839</Number> </Feature> </ABC> <ABC id="2"> <Feature> <Number>82783</Number> </Feature> </ABC> </doc>
Output:
<Key="1234"> <File0> <ABC id="File0-1"> <Feature> <Number>86839</Number> </Feature> </ABC> <ABC id="File0-2"> <Feature> <Number>82783</Number> </Feature> </ABC> </File0> <Status="in use"/> <Features> <File1> <ABC id="File1-1"> <Feature> <Number>86839</Number> </Feature> </ABC> <ABC id="File1-2"> <Feature> <Number>82783</Number> </Feature> </ABC> </File1> <File2> <ABC id="File2-1"> <Feature> <Number>86839</Number> </Feature> </ABC> <ABC id="File2-2"> <Feature> <Number>82783</Number> </Feature> </ABC> </File2> </Features> <Other_Status/> </Key>
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: copy XML elements from one file to another
by Anonymous Monk on Nov 25, 2013 at 15:57 UTC | |
|
Re^2: copy XML elements from one file to another
by Anonymous Monk on Nov 25, 2013 at 17:21 UTC | |
by kcott (Archbishop) on Nov 26, 2013 at 05:09 UTC | |
by Anonymous Monk on Nov 26, 2013 at 20:02 UTC | |
by kcott (Archbishop) on Nov 27, 2013 at 05:25 UTC |