I use this perl code to read XML from a file, and then write to another file (my full script has code for adding attributes):
#!usr/bin/perl -w use strict; use XML::DOM; use XML::Simple; my $num_args = $#ARGV + 1; if ($num_args != 2) { print "\nUsage: ModifyXML.pl inputXML outputXML\n"; exit; } my $inputPath = $ARGV[0]; my $outputPath = $ARGV[1]; open(inputXML, "$inputPath") || die "Cannot open $inputPath \n"; my $parser = XML::DOM::Parser->new(); my $data = $parser->parsefile($inputPath) || die "Error parsing XML File"; open my $fh, '>:utf8', "$outputPath" or die "Can't open $outputPath for writing: $!\n"; $data->printToFileHandle($fh); close(inputXML);
however, this does not preserve characters such as line breaks. For example, this XML:
<?xml version="1.0" encoding="utf-8"?> <Test> <Notification Content="test1 testx 
test2
test3
" Type="Test1234"> </Notification> </Test>
becomes the following:
<?xml version="1.0" encoding="utf-8"?> <Test> <Notification Content="test1 testx test2 test3 " Type="Test1234"> </Notification> </Test>
I suspect that I am not writing correctly.
source share