My XML file looks like this:
<A>
<file>data1</file>
<path>data2</path>
</A>
<B>
<file>data3</file>
<path>data4</path>
</B>
So I read the data from a log file, parse it and get tags like A, B, file and path. Right now I use a for loop to iterate over each outer tag, and then compare against each sub tag to see if the data exists in the XML file.
$data = $xml->XMLin("xmlfile");
foreach $e ( $data->{$outerTag} ) # outertag could be A, B
{
if( $e->{file} eq $fname ) { do_something } else { return 0; }
if( $e->{path} eq $pname ) { do_something } else { return 0; }
}
Is there a way whereby I don’t have to use the for loop.? Say I could do something like this (I am making this up):
if( $data->{$outerTag}->{$fname} ) { do_something } else { return 0; }
As long as
XML::Simplehas built a rational structure for your data you can writebut, depending on what you want to do with the data, you may well be better off using a better XML parser, like
XML::LibXML.