Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Search and replace again

by ikegami (Patriarch)
on Apr 19, 2010 at 21:42 UTC ( [id://835601]=note: print w/replies, xml ) Need Help??


in reply to Re: Search and replace again
in thread Search and replace again

use strict; use warnings; use XML::LibXML qw( ); my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($ARGV[0]); my $root = $doc->documentElement(); for my $node ($root->findnodes('//image[@scope="local"]')) { $node->removeAttribute('scope'); } binmode STDOUT; print $doc->toString();

I'm not even gonna try an XML::Simple solution.

Replies are listed 'Best First'.
Re^3: Search and replace again
by crashtest (Curate) on Apr 20, 2010 at 00:46 UTC

    I was curious why you binmode STDOUT in your code (and in your other example) - is it to ensure you have UNIX line endings (and not CRLFs) in the output if Perl is running on Windows?

      For starters, XML is a binary format. binmode definitely won't hurt anything.

      binmode doesn't just disable :crlf; it disables any :encoding too.

      You probably should always use binmode or equivalent (e.g. use open), either to remove layers* when you want to ensure the bytes are unmolested*, or to add some when you want to output text.

      * — These may be added via $ENV{PERLIO}, via -C, or by Perl itself as the case is for :crlf on Windows.

        "XML is a binary format" - not to nitpick (except I will), but that's just not right. From http://www.w3.org/XML/:

        Extensible Markup Language (XML) is a simple, very flexible text format derived from SGML (ISO 8879).
        (emphasis mine) Obviously at some point the XML text has to be encoded to a binary format. XML::LibXML's toString method (when called on a document) does do that, so at that point, you are indeed dealing with binary data and should turn off any PerlIO layers on your output handle, as you did in your example. I didn't realize that $doc->toString returned binary data.

Log In?
Username:
Password:

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

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

    No recent polls found