how to parse a xml which might have zero to N number of nodes using perl

1

How to handle a xml which might have one or more occurance of node in the file in perl.

In the current solution provided below,

foreach $FileContainerRec (@{$XmlFile->{FileContainer}})
{
    $FileContainer           = 'C'.$FileContainerCt;
     foreach $FileRec (@{$FileContainerRec->{File}}) 
     {
       $File                   = $FileContainer.'file'.$FileCt;
       $OutFileName            = $FileRec->{OutFileName};
       $ActiveIndicator        = $FileRec->{ActiveIndicator};
       $AuditFlag              = $FileRec->{AuditFlag};
       $Delimiter              = $FileRec->{Delimiter};
       @FileDesc=();
       @FileDesc=($OutFileName,$ActiveIndicator,$AuditFlag,$Delimiter);
       $FileList{$File}=[@FileDesc];
       $FileCt++;
     }
     $FileContainerCt++;
     $FileCt=1;
} 

sample ip xml:

<Document>
<FileContainer>
<File>
......
</File>
<File>
......
</File>
</FileContainer>
<FileContainer>
<File>
......
</File>
<File>
......
</File>
</FileContainer>
<Document>

the ouccurance of and May range from 0 to n number of times.

The perl code snippet throws out error as 'Not an ARRAY Referance', when we have either One 'FileContainer' or 'File' Nodes.

Shon Cleneeto

Posted 2017-07-11T11:04:31.610

Reputation: 21

1Any reason why you're not using one of the many XML parsing libraries already available? – mtak – 2017-07-11T11:49:59.963

To the close voter, Perl questions are on topic on [su]. – DavidPostill – 2017-07-11T11:58:37.463

You should avoid XML::Simple. The documentation for the module itself says this: The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative. If you edit your question to show some usable XML data then I'll write some sample code.

– Borodin – 2017-08-08T12:49:09.790

@mtak: "Any reason why you're not using one of the many XML parsing libraries already available?" It looks to me like the OP is using XML::Simple. – Borodin – 2017-08-08T12:49:52.020

We can't really help you without a description of the problem and some valid XML data. *If you edit your question to show some usable XML data then I'll write some sample code* – Borodin – 2017-08-19T19:05:20.820

No answers