How can I write XML data to a file with Perl?

2019-07-23 11:39发布

Could you please correct my code below.

#!/usr/local/bin/perl

open (MYFILE, '>>data.xml');
print MYFILE "<?xml version="1.0" encoding="UTF-8"?>\n";
close (MYFILE); 

Updated.

#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE '<?xml version="1.0" encoding="UTF-8"?\>'."\n";
print MYFILE '<?xml version="1.0" encoding="UTF-16"?\>'."\n";
close (MYFILE);

output: working well now.

<?xml version="1.0" encoding="UTF-8"?\>
<?xml version="1.0" encoding="UTF-16"?\>

BUT.

#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE '<?xml version="1.0" encoding="UTF-8"?\>'.'\n';
print MYFILE '<?xml version="1.0" encoding="UTF-16"?\>'.'\n';
close (MYFILE);

Output: # error format with \n

<?xml version="1.0" encoding="UTF-8"?\>\n<?xml version="1.0" encoding="UTF-16"?\>\n

标签: xml perl
8条回答
闹够了就滚
2楼-- · 2019-07-23 12:01

The problem is you have unescaped quotes in the string. Either escape the quotes using the backslash or surround your print string with qq{}:

print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>\n};
--or--
print MYFILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
查看更多
贼婆χ
3楼-- · 2019-07-23 12:03
print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>\n};

Your problem is that you had nested double quotes; using qq{} to delimit the string will solve this issue.

查看更多
Viruses.
4楼-- · 2019-07-23 12:04

I would recommend to use:

use XML::Simple;
查看更多
Root(大扎)
5楼-- · 2019-07-23 12:07

Always turn on warnings and strictures, so you find out earlier what went wrong, and get more details why:

#!/usr/local/bin/perl
use strict;
use warnings;

Always use the lexical-variabled, three-argument form of open (there's a big discussion why over here), and always check the return value (it will return an error if something went wrong, and put the reason why in the $! variable (see under $! at perldoc perlvar). Also, die will print the line number of where the program quit if you don't end your string with a \n (more at perldoc -f die).

open my $file, '>>', 'data.xml' or die "Can't open file: $!";

And use double-quotes around the \n so that it is printed properly:

print $file '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
print $file '<?xml version="1.0" encoding="UTF-16"?>' . "\n";
close $file;
查看更多
SAY GOODBYE
6楼-- · 2019-07-23 12:08

Always, always, ALWAYS check the value returned from open, e.g.,

open (MYFILE, '>>data.xml')
  or die "$0: open: $!";

Note the important bits in the error message:

  1. the name of the program that failed, $0
  2. what it was trying to do, open
  3. why it failed, $!

Without a newline at the end of the string passed to die, it appends the file and line number where your program died.

查看更多
Ridiculous、
7楼-- · 2019-07-23 12:10

Here is some code of mine for printing an XML file:

open(XML, ">$xmlfile");
print XML (<<EOF);
<?xml version="1.0" encoding="UTF-8"?>
<gpx
 version="1.1"
 creator="Navaid Waypoint Generator - http://navaid.com/GPX/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.topografix.com/GPX/1/1"
 xmlns:navaid="http://navaid.com/GPX/NAVAID/1/0"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd
 http://navaid.com/GPX/NAVAID/1/0 http://navaid.com/GPX/NAVAID/1/0">
 <metadata>
    <author>
        <name>Paul Tomblin</name>
        <email id="ptomblin" domain="xcski.com"/>
        <link href="http://blog.xcski.com/"/>
    </author>
    <link href="http://navaid.com/GPX/"/>
</metadata>
EOF
查看更多
登录 后发表回答