Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: XML::Twig Help

by AndyZaft (Hermit)
on Jul 06, 2010 at 15:24 UTC ( [id://848248] : note . print w/replies, xml ) Need Help??

in reply to XML::Twig Help

Can you show the XML that gets opened by that Twig? First, lets make sure that whatever gets opened actually has a djn-geo tag in there. This error comes from the set_handler sub, so you could also try just opening the XML without any handlers set and print it. Maybe what you are opening is not what you think you are opening.

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

    The djn-geo tag is in there, for example:

    <djn-geo> <\c>R/ALT\<\/c> <\c>R/ARA\<\/c> </djn-geo>

    My code now reads:

    #!/usr/bin/perl use strict; use warnings; chomp(my $enter = <STDIN>); my @fields = split(/ /, $enter, 9999); use XML::Twig; open (FILE, $fields[1]); my $file = <FILE>; my $t= XML::Twig->new( twig_roots => { 'djn-geo' => 1}); $t->parsefile($file); open(STDOUT, ">$fields[2]"); $t->print;

    And I get the error:

    Unsuccessful stat on filename containing newline at C:/Perl/site/lib/XML/ line 684, <FILE> line 1. Couldn't open version="1.0" encoding="ISO-8859-1"?> : Invalid argument at C:\Documents and Settings\odeab\My Documents\Exec_Extract\tw line 10 at C:\Documents and Settings\odeab\My Documents\Exec_Extract\ line 10

      You still aren't checking that open worked.

      Well, before we go further, Twig will tell you that it is not well-formed, likely because  <\c> is not a valig tag. If your XML is filled with tags like that even if you manage to open the files themselves you will run into problems.

      From your code I assume you are trying to type in a few XML filenames and the process them. First problem, if you only have 1 XML as a test, you need to open fields[0], otherwise open will fail with uninitialized value. Then once you fix that you are trying to open a file that's named the first line of the first file. You also don't need to open a file for XML::Twig, the module can do it itself. I would suggest starting simple, like take 1 XML file and create a simple perl script that opens it and does something with it. Something along the lines of:

      #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $file = "yourxmlfilename.xml"; my $t= XML::Twig->new( twig_roots => { 'djn-geo' => 1}); $t->parsefile($file); $t->print;
      Based on more assumptions looking at your code that you are trying to output to the second file given in the input, you can redirect later or at command line. Although I'm not sure how that behaves on windows nowadays. In any case you can also use  $t->print_to_file($anotherfilename) and many other ways.