Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: XML::Twig Help

by mirod (Canon)
on Jul 06, 2010 at 16:23 UTC ( #848266=note: print w/replies, xml ) Need Help??

in reply to XML::Twig Help

The error you get comes from the fact that you need to quote djn-geo, as it doesn't look like a bareword to Perl.

That said there are many other problems with your code. I suspect what you want is something like this (untested):

#!/usr/bin/perl use strict; use warnings; use XML::Twig; # get the user input. die if it doesn't look right my $enter = <STDIN>; my @fields = split(/ /, $enter); if( @fields != 2) { die "usage $0 <input_file> <output_file>\n"; } # no need to open the file, quote djn-geo my $t= XML::Twig->new( twig_roots => { 'djn-geo' => 1}); $t->parsefile($fields[0]); # print_to_file was added in XML::Twig 3.23 $twig->print_to_file( $fields[1]);

Replies are listed 'Best First'.
Re^2: XML::Twig Help
by shravnk (Initiate) on Jul 06, 2010 at 17:17 UTC


    This has been enormously helpful, and your code works when I process one tree at a time. However, the xml doc that I am trying to work with contains hundreds of trees with the same format. How can I get the XML::Twig module to iterate over many trees within one document. (This will be my last question, I promise)

    Thanks again


      There should not be more than 1 root element in a proper XML file. If that is what you mean by "many trees", otherwise mirod has a few posts here on pm regarding how to process XML in chunks. When I started with XML::Twig, I read all his posts here and that answered every question. If you have perldoc, you can look at the twig page and search for flush and purge. I can't seem to reach for the last week from around here, so I can't link those examples.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-08-16 12:43 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (167 votes). Check out past polls.