1

I have to read a huge data base of xml files, using XML::Simple.

my $xml = XMLin("${file}", ForceArray => 1, KeyAttr => {});

the problem is that some of those xml are corrupted and I would like my program to delete those and keep running. I have not been able to find any way to catch the error comming from XMLin parser error : Start tag expected '<' not found

Any idea ?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Arcyno
  • 4,153
  • 3
  • 34
  • 52

1 Answers1

3

First off - please don't use XML::Simple - it's deeply nasty. Why is XML::Simple "Discouraged"?

To address your point though - broken XML is supposed to be fatal to the parser - it must die (by design). The way you handle this in perl is wrap it in an eval block, and catch $@ to see if it failed:

my $xml = eval { XMLin("${file}", ForceArray => 1, KeyAttr => {}); };
warn $@ if $@; 
Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101