Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Generating .xml file after creating xml data from XML::LibXML

by balajinagaraju (Sexton)
on Apr 03, 2013 at 06:57 UTC ( [id://1026789]=perlquestion: print w/replies, xml ) Need Help??

balajinagaraju has asked for the wisdom of the Perl Monks concerning the following question:

HI, I want to create a .xml file output after some post proccessing in my script and i am using XML::LibXML to do that , but it prints data as a string to the console. How do i get the output in an XML file?. I referred to XML::LibXML module documentation on CPAN and i din't find anything about this. Can anyone suggest how i can generate a .xml file. Below is what i am doing.

my $doc1 = XML::LibXML::Document->new('1.0', 'utf-8'); my $root = $doc1->createElement("TestSummary"); $root->setAttribute('Test'=> 'Sprint12'); my %tags = ( resultcount => $resultcount, testcasecount => $testcasecount, ); for my $name (keys %tags) { my $tag = $doc1->createElement($name); my $value = $tags{$name}; $tag->appendTextNode($value); $root->appendChild($tag); } $doc1->setDocumentElement($root); print $doc1->toString();

Output is generated in the console as below

<?xml version="1.0" encoding="utf-8"?> <TestSummary some-attr="some-value"><resultcount>7</resultcount><testc +asecount>9</testcasecount></TestSummary>

The last statement is responsible to print it as a string but i want to generate an xml file from my output

Replies are listed 'Best First'.
Re: Generating .xml file after creating xml data from XML::LibXML
by hdb (Monsignor) on Apr 03, 2013 at 07:09 UTC

    Instead of

    print $doc1->toString();

    you do

    open XML, ">some.xml"; print XML $doc1->toString(); close XML;

    UPDATE: there is also a toFH function according to the documentation of XML::LibXML:

    # save open my $out, '>', 'out.xml'; binmode $out; # as above $doc->toFH($out); # or print {$out} $doc->toString();

      Thanks for your response, your suggestion worked

Re: Generating .xml file after creating xml data from XML::LibXML
by Corion (Patriarch) on Apr 03, 2013 at 07:12 UTC

    See open and print on how to print the output to a file instead of the console.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1026789]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-25 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found