|
Posted by Ted Johnson on January 29, 2005, 12:45 pm
Please log in for more thread options
Using XML::Simple, what's the best way to check for
the existence of a particular XML tag?
For example, lets say I have 2 files, "success.xml" and
"failure.xml" which differ only in their innermost tags
(one being "<responseSuccess>" and the other being "<responseFailed>").
Can I use "exists()" or "defined()" on the path which refers
to that tag?
For example, consider the following:
------ START: success.xml -------
<?xml version="1.0" encoding="UTF-8"?>
<response>
<header>
<responseStatus>
<responseSuccess>
It worked, blah blah
</responseSuccess>
</responseStatus>
</header>
</response>
------ END: success.xml -------
------ START: failure.xml -------
<?xml version="1.0" encoding="UTF-8"?>
<response>
<header>
<responseStatus>
<responseFailed>
It failed, blah blah blah
</responseFailed>
</responseStatus>
</header>
</response>
------ END: failure.xml -------
Is this the correct way to check for tag existence? :
#!/opt/perl/bin/perl
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
$data = $xml->XMLin("./$ARGV[0]");
if (defined( $data->
->->)) {
# then it worked
}
elif (defined($data->
->->)) {
# then it failed
}
or is there a better way?
Thanks in advance.
-Ted
PS. I have no control over the format of the XML files - they come
from another app.
|