简体   繁体   中英

How can I save an XML DOM tree to a new document using Perl's XML::LibXML?

I write the following (DOM) Perl script (shown below) in order to create the following XML DOM:

 <books>
 <computer/>
 </books>

How can I save the XML output into test.xml file? I tried to save the XML with

 $doc->printToFile('/tmp/test.xml'); 

but I get:

can't locate object method "printToFile" via package "XML::LibXML::Document"

The Perl script:

 #!/usr/bin/perl

 use XML::LibXML;
 my $doc;
 $doc = XML::LibXML::Document->new;

 my $objbooks = $doc->createElement('books');
 $doc->appendChild($objbooks);

 my $objcomputer = $doc->createElement('computer');
 $objbooks->appendChild($objcomputer);

I think you want XML::LibXML::Document::toFile :

  $state = $doc->toFile($filename, $format); 

This function is similar to toString() , but it writes the document directly into a filesystem. This function is very useful, if one needs to store large documents.

The format parameter has the same behaviour as in toString() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM