in reply to Parsing an XML file with an undefined value

You could try something like this,
for my $entry ($doc->getElementsByTagName('Entry')) { FIELD: for my $field (qw(test1 test2 test3 test4 test5)) { my $test = $entry->getElementsByTagName( $field )->item(0); if (not $test) { warn "No item found for tag $field\n"; next FIELD; } warn "Node Value for $field => " . $text->getFirstChild->getNo +deValue . "\n"; } }

Replies are listed 'Best First'.
Re^2: Parsing an XML file with an undefined value
by ravishi (Acolyte) on Nov 12, 2008 at 19:25 UTC
    Thanks to both of you. I figured it out based on your comments. This is what I did:
    my $text = qw/test4/; $test4 = $entry->getElementsByTagName( $text)->item(0); if (defined $test4) { $test4 = $entry->getElementsByTagName( $text)->item(0)->getFirstCh +ild->getNodeValue; }
    Reps to both of you!
      I am a novice to perl and trying to implement this approach in my code. I am using ActivePerl on Windows. 2 questions: 1. I am getting an error stating that "parsefile" is undefined. I have defined the following modules in my code:
      use strict; use XML::Twig; use File::Basename; use XML::DOM;
      since I use parsefile through Twig for my previous code, would this be the cause of my error? I think it's interfering with calling parsefile through DOM. 2. Would this approach also work if I want to validate attributes instead of tags in an XML file? Thanks
        copy/paste the error message