Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

XML::Twig Question

by l.frankline (Hermit)
on Apr 22, 2006 at 06:31 UTC ( [id://545023]=perlquestion: print w/replies, xml ) Need Help??

l.frankline has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am able to print the xml file with pretty view option. It's working fine, but I am not able to write the output file into my disk.

I have searched the option in XML::Twig module, but I couldnt get it.

so far, I have tried the below code..

use XML::Twig; my $twig=XML::Twig->new(pretty_print=> 'indented'); $twig->parsefile( 'sample.xml'); $data = $twig-print; open OUT, ">" . "pretty.xml"; print OUT $data; close OUT; print "\nover\n";

Please give me your suggestions

Thanks
Frank

Don't put off till tomorrow, what you can do today.

Replies are listed 'Best First'.
Re: XML::Twig Question
by mirod (Canon) on Apr 22, 2006 at 07:01 UTC

    There are 2 ways to do this:

    my $data= $twig->sprint; print OUT $data;

    or

      $twig->print( \*OUT)

    A simple $twig->print prints to the standard output (or whatever filehandle is select-ed at the time).

    Just in case... note that you shouldn't use bareword as filehandles, but rather variables as in:

    open( my $out, '>pretty.xml') or die "can't open pretty.xml: $!"; # +perl 5.6.*

    or

    open( my $out, '>', 'pretty.xml') or die "can't open pretty.xml: $!" +; # perl 5.8.*

    Perl's Best Practice has a very good explanation of the reasons for this, but in a nutshell, OUT is a global variable, which is bad.

Re: XML::Twig Question
by kvale (Monsignor) on Apr 22, 2006 at 06:38 UTC
    One error is that "$twig-print" should be "$twig->print".

    -Mark

Re: XML::Twig Question
by Samy_rio (Vicar) on Apr 22, 2006 at 06:56 UTC

    Hi l.frankline, use $data = $twig->sprint; instead of $data = $twig-print;

    Anyway $data = $twig-print; is typo. XML::Twig module description as:

    =item sprint Return the text of the whole document associated with the twig. To be used only AFTER the parse.

    =item sprint ($elt, $optional_no_enclosing_tag) Return the xml string for an entire element, including the tags. If the optional second argument is true then only the string inside the element is returned (the start and end tag for $elt are not). The text is XML-escaped: base entities (& and < in text, & < and " in attribute values) are turned into entities.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: XML::Twig Question
by davido (Cardinal) on Apr 22, 2006 at 15:33 UTC

    This may not be the issue in this particular case, but it is an issue that you should address in your code: Always check the return values of system calls.

    What does that mean? It means, when you ask Perl to open a file, you should check to be sure that the file actually got opened. Here is how that is often accomplished:

    open OUTFILE, '>', 'pretty.xml' or die "Couldn't open pretty.xml for output:\n$!\n"; # ... and later... close OUTFILE or die "Couldn't close output file, pretty.xml:\n$!\n";

    Also notice, within your open command you are using the concatenation operator, '.', when it's actually better in this case to use the comma.


    Dave

Log In?
Username:
Password:

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

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

    No recent polls found