I am using Perl and XML::DOM to parse an XML file. In the XML file, there are tags that don't appear in all of the entries. How do I check to see if they are there before I try to read in the value. I have tried a bunch of things and they don't seem to work. I always get a value of "Can't call method "getFirstChild" on an undefined value at...". I have posted a sample code with the associated xml file.

PERL FILE:

#!/usr/bin/perl -w # File displayPipeTracer2.pl use XML::DOM; use Getopt::Long; my $testXML = qw/textXML.xml/; my $test4 = ""; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ($testXML); foreach my $entry ($doc->getElementsByTagName('Entry')) { my $test1 = $entry->getElementsByTagName('test1')->item(0)->getFir +stChild->getNodeValue; my $test2 = $entry->getElementsByTagName('test2')->item(0)->getFir +stChild->getNodeValue; my $test3 = $entry->getElementsByTagName('test3')->item(0)->getFir +stChild->getNodeValue; #if (defined $entry->getElementsByTagName('test 4')->item(0)->getF +irstChild->getNodeValue) { # $test4 = $entry->getElementsByTagName('test4')->item(0)->getF +irstChild->getNodeValue; #} print "$test1, $test2, $test3"; print ", $test4" if defined $test4; print "\n"; }
XML FILE:
#!/usr/bin/perl -w # File displayPipeTracer2.pl use XML::DOM; use Getopt::Long; my $testXML = qw/textXML.xml/; my $test4 = ""; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ($testXML); foreach my $entry ($doc->getElementsByTagName('Entry')) { my $test1 = $entry->getElementsByTagName('test1')->item(0)->getFir +stChild->getNodeValue; my $test2 = $entry->getElementsByTagName('test2')->item(0)->getFir +stChild->getNodeValue; my $test3 = $entry->getElementsByTagName('test3')->item(0)->getFir +stChild->getNodeValue; #if (defined $entry->getElementsByTagName('test 4')->item(0)->getF +irstChild->getNodeValue) { # $test4 = $entry->getElementsByTagName('test4')->item(0)->getF +irstChild->getNodeValue; #} print "$test1, $test2, $test3"; print ", $test4" if defined $test4; print "\n"; }

In reply to Parsing an XML file with an undefined value by ravishi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.