tags:

views:

33

answers:

1

Hi all

I create the following XML file, by perl script (Showing down) , using XML::LibXML:

 more test.xml 

 <?xml version="1.0"?>
 <books>
 <computer/>
 </books>

My question: how to remove "xml version title":

      <?xml version="1.0"?>

from the test.xml file? With DOM commands in the perl script?

in order to get only the follwoing lines in the text.xml file:

 <books>
 <computer/>
 </books>

Yael

#

the perl script:

#!/usr/bin/perl

use strict;
use warnings;
use XML::LibXML;

my $doc  = XML::LibXML::Document->new;  
my $root = $doc->createElement('books');
$doc->setDocumentElement($root);

my $computer = $doc->createElement('computer');
$root->appendChild($computer);
$doc->toFile('/var/tmp/test.xml'); 
A: 

Okay, regarding my previous comment, I now found a solution.

It seems toFile bypasses $skipXMLDeclaration whereas toString doesn't. So the following works:

$XML::LibXML::skipXMLDeclaration = 1;

my $doc  = XML::LibXML::Document->new;
# create your document

print $doc->toString;

The (very small) downside is that you have to write the file yourself.

musiKk
THX can you please advice the best way to print $doc->toString; to file ( in order to save in the file the XML doc?yael
yael
C'mon, this isn't hard. You just open a file handle and print to it... `open my $fh, '>', $filename; print $fh $doc->toString; close $fh;`
musiKk