in reply to Pasing an XML file to generate a certain output.

Where in the heck did this file come from?   If it’s not valid XML, then maybe there’s a rather serious bug in the program that generated it, and that ought to be the place to begin.   If a generated-output file isn’t valid XML, then I quite frankly would not trust the rest of its content, either.

Beyond that, yes:   use some XML parser.   There are two types:   those which slurp the entire file into memory, and those that don’t.   Either way, this frees you from trying to monkey with regular-expressions yourself.

Given that your available-memory is probably much larger than the file, strongly consider using “XPath expressions,” which are a sort of query-language for XML files, implemented e.g. by XML::LibXML.   This technique avoids you having to write program-logic that mimics the structure of the file you wish to process.   You simply write an XPath string that describes what you are looking for, and the library returns them all to you as a [Perl ...] data-structure.