Hi,
I'm trying to write a script for some xml file handling, but I'm not getting too far with it.
I've got the following xml content
<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Operation name="OPER1">
<Action name="ACTION1">
<Condition name="None">
<Parameter name="AAA">
</Parameter>
</Condition>
</Action>
<Action name="ACTION2">
<Action-category name="Normal">
<Condition name="All">
<Parameter name="AAA">
</Parameter>
</Condition>
</Action-category>
</Action>
</Operation>
</Test>
This is just a small piece of the actual file that can be thousands of lines.
Some "Actions" can have "Action-category" but some do not, and I would like to be able to do different processing based on that. I am however unable to test if this element exists.
My code is as follows:
#!/usr/bin/perl
use XML::DOM;
use strict;
my @files = @ARGV;
my $operation = "OPER1";
my $parser = XML::DOM::Parser->new();
my $doc = $parser->parsefile(@files[0]);
foreach my $oper ($doc->getElementsByTagName('Operation'))
{
print "OPERATION: " . $oper->getAttribute('name') . "\n";
if ( $operation eq $oper->getAttribute('name'))
{
foreach my $action ($oper->getElementsByTagName('Action'))
{
print "ACTION: " . $action->getAttribute('name') . "\n";
if (defined ($action->getElementsByTagName('Action-category')))
{
print "Action-category defined\n";
}
else
{
print "Action-category NOT defined\n";
}
}
}
else
{
#do something else
}
}
and it results in to the following output:
>./parser.pl file.xml
OPERATION: OPER1
ACTION: ACTION1
Action-category defined
ACTION: ACTION2
Action-category defined
So, it says the "action-category" is defined, even though it is not? :wall:
Would anyone have any idea why or how to fix this?